Describe
In a stingy country there are n cities, these n cities only N-1 Road to connect this n city. Now, Tom is in the city of S, he has a map of the country, he wants to know if he is going to visit the city of T, the first city that must go through is the number of cities (assuming you do not go to repeat the road). Enter the first line enter an integer m to represent the total m (1<=m<=5) Group of test data
The first line of each set of test data enters a positive integer n (1<=n<=100000) and a positive integer S (1<=s<=100000), n represents the total number of cities, and S indicates the number of the visitors ' city.
Subsequent N-1 lines, each row has two positive integers a,b (1<=a,b<=n), which means that there is a road connection between the city of number A and City B. The output of each set of test data is n positive integers, where the number of I represents the number of the last city that must go through when s goes to city I. (When i=s, please output-1) sample input
1
1 1 9 1 8 8 Ten 3 8 6 1 2
ten 4
9 5 3 7
Sample output
-1 1 10 10 9 8 3 1 1 8
Because the number of the given city N is too large, it takes a #include<vector>,vector to build an array, Vector<int>a is an array of integers similar to int a[], except that his length is indeterminate, A.size () can be used to read his length.
and Vector<int>a[max] is a two-dimensional array, but the first dimension is fixed (no more than max), two-dimensional size is not fixed, this problem is used vector is to use his indefinite length, if the direct establishment of two-dimensional array a[n][n], N is too large, such a two-dimensional array is absolutely beyond memory.
(1) header file #include<vector>.
(2) Creating a Vector object,vector<int> VEC;
(3) Tail Insert number: Vec.push_back (a);
(4) Use subscript to access elements,cout<<vec[0]<<endl; remember that subscripts start at 0.
(5) Vector size: vec.size ();
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int f[100100];
vector<int>v[100005];
void Dfs (int s)
{
int i;
for (i=0; I<v[s].size (); i++)
{
if (f[v[s][i)])
continue;
F[v[s][i]]=s;
DFS (V[s][i]);
}
int main ()
{
int i,j,n,s,t,a,b;
scanf ("%d", &t);
while (t--)
{
memset (f,0,sizeof (f));
memset (v,0,sizeof (v));
scanf ("%d%d", &n,&s);
F[s]=-1;
For (I=1 i<n; i++)
{
scanf ("%d%d", &a,&b);
V[a].push_back (b);
V[b].push_back (a);
}
DFS (s);
For (I=1 i<=n; i++)
{
printf ("%d", F[i]);
if (i==n)
printf ("\ n");
else printf ("");
}
}
return 0;
}