Mean country time limit: 1000 MS | memory limit: 65535 KB difficulty: 3 Description in a mean country in N cities, the N cities have only N-1 routes to connect the N cities. Now, Tom is in city S. He has a map of the country. He wants to know if he wants to visit city T, the first city that must pass through is the city number (Suppose you do not walk the same road ). Enter an integer M in the first line to indicate that the test data has a total of M (1 <= M <= 5) enter a positive integer N (1 <= N <= 100000) and a positive integer S (1 <= S <= 100000) In the first line of each group of test data ), N indicates the total number of cities, S indicates the number of the city where the visitor is located, followed by the N-1 line, each line has two positive integers a, B (1 <= a, B <= N ), there is a road connection between City a and City B. Output N positive integers for each group of test data. The I number indicates the number of the previous city to pass from S to I. (When I = S, output-1) sample input 110 11 91 88 1010 38 61 210 49 53 7 sample output-1 1 10 10 9 8 3 1 8 source classic question [cpp] ****** * *************************** Date: 2013-3-26 * Author: SJF0115 * question: Question 20: Mean country * Source: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 20 * result: AC * Source: Nanyang sci-tech OJ * Summary: * *********************************/# include <stdio. h> # include <iostream> # include <vector> # include <string. h> using namespace std; vector <int> G [100001]; int preCity [100001]; int vis [100001]; // Deep Search void DFS (int location) {vis [location] = 1; // access the city connected to location (int I = 0; I <G [location]. size (); I ++) {int v = G [location] [I]; if (! Vis [v]) {// store the preCity [v] = location of the city to access; // printf ("% d \ n", location, v ); DFS (v) ;}} int main () {int N, M, City, Location, a, B, I, first; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt "," r ", stdin); scanf (" % d ", & N); while (N --) {scanf ("% d", & City, & Location); // initialize for (I = 1; I <= City; I ++) {G [I]. clear () ;}// input path for (I = 1; I <City; I ++) {scanf ("% d", & a, & B ); G [a]. push_back (B); G [B]. push_back ();}/ /Access the city memset (vis, 0, sizeof (vis); vis [Location] = 1; preCity [Location] =-1; DFS (Location ); // output first = 1; for (I = 1; I <= City; I ++) {if (first) {first = 0 ;} else {printf ("");} printf ("% d", preCity [I]);} printf ("\ n");} return 0 ;} /********************************** Date: 2013-3-26 * Author: SJF0115 * question: Question 20: Mean country * Source: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 20 * result: AC * Source: Nanyang sci-tech OJ * Summary: * *********************************/# include <stdio. h> # include <iostream> # include <vector> # include <string. h> using namespace std; vector <int> G [100001]; int preCity [100001]; int vis [100001]; // Deep Search void DFS (int location) {vis [location] = 1; // access the city connected to location (int I = 0; I <G [location]. size (); I ++) {int v = G [location] [I]; if (! Vis [v]) {// store the preCity [v] = location of the city to access; // printf ("% d \ n", location, v ); DFS (v) ;}} int main () {int N, M, City, Location, a, B, I, first; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt "," r ", stdin); scanf (" % d ", & N); while (N --) {scanf ("% d", & City, & Location); // initialize for (I = 1; I <= City; I ++) {G [I]. clear () ;}// input path for (I = 1; I <City; I ++) {scanf ("% d", & a, & B ); G [a]. push_back (B); G [B]. push_back (a);} // access the city memset (vis, 0, sizeof (vis); vis [Location] = 1; preCity [Location] =-1; DFS (Location); // output the first station of the access City first = 1; for (I = 1; I <= City; I ++) {if (first) {first = 0;} else {printf ("");} printf ("% d", preCity [I]);} printf ("\ n ");} return 0 ;}