Uploaded by
Zhang Yunzun
#include <stdio.h> #include <string.h> #include <vector>using namespace std; #define N 100005int Tree[n ];vector<int>map[n];void changetree (int root,int flag) {for (int i=0;i<map[root].size (); i++) {if (map[root][i ]!=flag) {tree[map[root][i]]=root;//Save the root node Changetree (map[root][i],root);//search from the root. }}}int Main () {int test,n,root,x,y;scanf ("%d", &test), while (test--) {memset (map,0,sizeof (map)); scanf ("%d%d", &n,&root); for (int i=0;i<n-1;i++) {scanf ("%d%d", &x,&y); Map[x].push_back (y); Map[y].push_back (x) ;} Tree[root]=-1; Changetree (Root,-1), for (int i=1;i<n;i++) printf ("%d", Tree[i]);p rintf ("%d\n", Tree[n]);} return 0;}
I also just understand today, to tell you about my understanding of the problems encountered in the process.
In fact, before making a diagram of the topic seniors let me read, I am not the kind of love reading. I like to find problems in the topic, and then surf the internet to read and solve. This is my way of learning.
Vector container, used for the first time. Understanding the vector array is a good idea, but I don't know why it becomes a two-dimensional array. where Map[i][j] may be more difficult to understand.
Example: map[1]=2,map[1]=3,map[1]=4; then map[1][0] equals 1,map[1][1]=3,map[1][2]=4. and map[1].size () = 3; (because map[1] only 2,3,4 three values);
The second not understand the place is no root tree converted to real roots tree, I wrote an example, and then slowly push according to the program, I understand. I hope you can try it yourself,
Well, the algorithm simply understands the idea, then the program is easy to write.