Home > Back-end >  PAT a1021 for help!!!!!!!!!!!!!!!!!!!!!!!!
PAT a1021 for help!!!!!!!!!!!!!!!!!!!!!!!!

Time:10-02

Topic: https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856

My code test point 2 and 4 test points not by  , about test point 2, I use the correct code change test found is wrong on the connecting piece of judgement, but I feel connected block test of several data quantity should be right? ! for bosses to help me look at it, looking for a half a day

I am novice, code style may be a long story also please correct me big haha

 # include 
#include
#include
#include
# define maxn 10010
using namespace std;
Vector G [maxn];//adjacency list
Vector Ans.//A and B and sets
Int n, maxdepth;//n: vertices maxdepth: the current maximum depth
Int A [maxn]={0}, B={0} [maxn];//A: starting from one node to traverse the entire tree can reach the deepest vertex B: starting from A select A node in the traversal tree can reach the deepest vertex
Bool visit [maxn];//whether the current vertex has been traversal
Bool CMP (int a, int b) {
Return a}
Int DFSnum;//number of the array
Void DFS (int u, int the depth, int array [maxn]) {//DFS calculation depth and nodes in the corresponding array
Visit [u]=true;
If (g [u] size ()==1 & amp; & Depth> Maxdepth) {
Memset (array, 0, sizeof (array));//if the current depth is greater than the maximum depth, to initialize array
Array [0]=u;//save the maximum depth vertex
DFSnum=1;//the re-initialization
Maxdepth=the depth;
[u]} else if (g size ()==1 & amp; & The depth=={maxdepth)
Array [DFSnum]=u;
DFSnum++;
}
for(int i=0; iInt v=g [u] [I];
If (visit [v]==false) {
DFS (v, the depth + 1, array);//recursive
}
}
}
Void DFS1 (int u) {//DFS1 check several connecting piece, do not calculate depth
Visit [u]=true;
for(int i=0; iInt v=g [u] [I];
If (visit [v]==false) {
DFS1 (v);
}
}
}
Int DFSTravel () {//look at several connecting piece
Int num.
for(int i=1; i<=n; I++) {
If (visit [I]==false) {
Num++;
DFS1 (I);
}
}
Return num.
}
Void init () {//initializes the
Memset (visit, false, sizeof (visit));
Maxdepth=0;
DFSnum=0;
}
Int main () {
The scanf (" % d ", & amp; n);
Int a, b;
for(int i=0; iThe scanf (" % d % d ", & amp; A, & amp; B);
G [a]. Push_back (b);
G [b] the push_back (a);
}
If (n==1) {//n 1 special processing
Printf (" 1 \ n ");
} else {
init();
Int anum=DFSTravel ();
If (anum!=1) {
Printf (" Error: % d components, "anum);
} else {
init();
DFS (1, 1, A);
init();
DFS (A [0], 1, B);
Int I=0, j=0;//and set A and B is all vertices
While (A [I]!=0) {
Ans. The push_back (A [I]);
i++;
}
While (B [j].=0) {
Ans. The push_back (B [j]);
j++;
}
Sort (Ans. The begin (), Ans. The end (), CMP);
Printf (" % d \ n ", Ans [0]).
for(int i=1; iIf (Ans [I]!=Ans] [I - 1) {//can't repeat output
Printf (" % d \ n ", Ans [I]);
}
}
}
}
return 0;
}



  • Related