cf219c hoosing Capital for Treeland

Source: Internet
Author: User

D. Choosing capital for Treelandtime limit per test3 secondsmemory limit per test256 megabytesinputstandard Inputoutputsta Ndard output

The country Treeland consists of N cities, some pairs of them is connected with unidirectional roads. Overall there is N -1 roads in the country. We know that if we don't take the direction of the the roads into consideration, we can get from any city to any other one.

The Council of the Elders have recently decided to choose the capital of Treeland. Of course it should is a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at the stage nobody I s thinking about getting back to the capital from these cities). For the reason if City a are chosen a capital, then all roads must being oriented so if we move along them, We can get from city A to any other city . For this some roads may has to be inversed.

The elders to choose, the capital so, they has to inverse the minimum number of roads in the country.

Input

The first input line contains integerN(2≤N≤2 105)-the number of cities in Treeland. Next n -1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of Integers  s i , Em>t i   (1≤ s i , t i n ;   s i t i )-the numbers of cities, Connected by that road. The  I -th road is oriented from City  s Span class= "Lower-index" > i  to city  t i . You can consider cities in Treeland indexed from 1 to  n .

Output

In the first line print the minimum number of roads to being inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital-a sequence of indexes of cities in the increasing order .

Examplesinput
3
2 1
2 3
Output
0
Input
4
1 4
2 4
3 4
Output
2

The topic surface of these paths will constitute a tree-like things, building edge is that the positive side is 0, the reverse is 1, the first DFS burst to seek out a point to flip the number of times, and then other points can be based on this point to ask, because when you know the answer to a point, the answer to the point that is connected to it, you ans[v]+ (u->v==0?1:-1), paint can know this, finished checking the other people's work, found that this is called the tree-shaped DP.

#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<stack>typedefLong Longll;#defineX First#defineY Second#defineMP (A, b) Make_pair (A, B)#definePB Push_back#defineSD (x) scanf ("%d",& (x))#definePi ACOs (-1.0)#defineSF (x) scanf ("%lf",& (x))#defineSS (x) scanf ("%s", (x))#defineMAXN 10000000#include<ctime>Const intinf=0x3f3f3f3f;Const Long LongMod=1000000007;using namespaceStd;vector< pair<int,int> >g[200005];intans[200005];intmn=inf;intDfsintEints) {    intans=0;  for(intI=0; I<g[e].size (); i++)    {        intNe=G[e][i].        X if(ne!=s) {ans+=dfs (ne,e) +G[e][i].        Y }    }    returnans;}voidSolveintEints) {     for(intI=0; I<g[e].size (); i++)    {        intNe=G[e][i].        X if(ne!=s) {Ans[ne]=ans[e]+ (G[e][i]. y==0?1:-1);        Solve (ne,e); }} MN=min (mn,ans[e]);}intMain () {#ifdef local freopen ("inch","R", stdin); //freopen ("Data.txt", "w", stdout);    int_time=clock (); #endif    intN; CIN>>N;  for(intI=1; i<n;i++)    {        ints,e; scanf ("%d%d",&s,&e); G[S].PB (MP (E,0)); G[E].PB (MP (s),1)); } ans[1]=dfs (1,0); Solve (1,0); cout<<mn<<Endl;  for(intI=1; i<=n;i++)    {        if(ans[i]==mn) printf ("%d", i); } #ifdef local printf ("Time :%d\n",int(Clock ()-_time)); #endif}
View Code

cf219c hoosing Capital for Treeland

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.