Nyoj20 Stingy Kingdom (no root tree converted to real roots tree)

Source: Internet
Author: User

    • topic
    • topic information
    • run result
    • line
    • discussion area
Stingy Country time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe
There are n cities in a stingy country, and only N-1 roads connect the n cities between 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).
Input
The first line enters an integer m to indicate that the test data is in 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 outputs n positive integers, where the number of I represents the number of the previous city that must pass from S to City I. (When i=s, please 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 1 8
Source
Classic Topics
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.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nyoj20 Stingy Kingdom (no root tree converted to real roots tree)

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.