Question 20: Mean country

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.