Nyoj 20 Stingy Country "DFS"

Source: Internet
Author: User

Stingy country time limit: Ms | Memory limit: 65535 KB Difficulty: 3 description In a stingy country there are N cities, and only the N-1 road between the N cities connects the N cities. Now, Tom in City number one, he has a map of the country, he wants to know if you want to visit the city of T, must go past the city is the number of cities (assuming you do not go the way of repetition). Enter the first line enter an integer m for the test data Total m (1<=m<=5) group
The first line of each set of test data is entered with 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 city where the visitor is located
The subsequent N-1 lines, each with two positive integers, a, B (1<=a,b<=n), indicate a road connection between City A and City B. Output each set of test data for n positive integers, where the number of I represents the number of the previous city that must pass from S to City I. (where 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

/* Puzzle:
Direct deep Search, the highlight is the use of vector storage, saving memory.
Method: Find high-quality code--reference--similarity degree 99.9%-->AC
*/

#include <cstdio>
#include <vector>
#include <cstring>
#define MAX 100002
using namespace Std;
vector<int>map[max];
int Pre[max];
void Dfs (int s)
{for
    (int i=0; i!=map[s].size (); ++i)
    {
        if (Pre[map[s][i])) continue;
        Pre[map[s][i]]=s;
        DFS (Map[s][i]);
    }
}
int main () {
    int m,n,s,a,b;
    scanf ("%d", &m);
    while (m--)
    {
         scanf ("%d%d", &n,&s);
         for (int i=1; i<n; i++)
         {
            scanf ("%d%d", &a,&b);
            Map[a].push_back (b);
            Map[b].push_back (a);
        }      
        DFS (s);
        Pre[s]=-1;
        for (int i=1; i<=n; i++)
            printf ("%d", Pre[i]);
        printf ("\ n");
        memset (map,0,sizeof (map));
         memset (pre,0,sizeof (pre));
    }
    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.