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. NextN?-? 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integerssI,?T i (1?≤? si,? Ti? ≤? n; s i? ≠? T i)-the numbers of cities, connected by that road. The i-th Road is oriented from city si to city T C31>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 21 2 3
The main point: To convert the direction to the weight of the edge, the forward Benquan value is 0, the reverse Benquan value is 1. With 1 as the root DFS, the total cost of 1 to all other points is obtained.
The focus is the second time Dfs:
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn=2e5+5;5 6 structnode{7 intTo,next,va;8}e[2*MAXN];9 Ten intN,tot; One intDP[MAXN],HEAD[MAXN]; A - voidInite () { -tot=0; thememset (head,-1,sizeof(head)); - } - - voidAddedge (intUintVintW) { +e[tot].to=v; -E[tot].va=W; +e[tot].next=Head[u]; Ahead[u]=tot++; at } - - voidDFS1 (intPaintu) { - for(inti=head[u];i!=-1; i=E[i].next) { - intv=e[i].to; - if(PA==V)Continue; in DFS1 (u,v); -dp[u]+=dp[v]+E[i].va; to } + } - the voidDFS2 (intPaintu) { * for(inti=head[u];i!=-1; i=E[i].next) { $ intv=e[i].to;Panax Notoginseng if(PA==V)Continue; -dp[v]+= (Dp[u]-dp[v]) + ((E[i].va)?-1:1); the DFS2 (u,v); + } A } the + intMain () - {inite (); $Memset (DP,0,sizeof(DP)); $ -scanf"%d",&n); - for(intI=2; i<=n;i++){ the intu,v; -scanf"%d%d",&u,&v);WuyiAddedge (U,v,0); theAddedge (V,u,1); - } Wu -DFS1 (0,1); AboutDFS2 (0,1); $ - inttemp=1e9; -vector<int>Q; - for(intI=1; i<=n;i++)if(dp[i]<temp) temp=Dp[i]; A for(intI=1; i<=n;i++)if(dp[i]==temp) q.push_back (i); +printf"%d\n", temp); the for(intI=0; I<q.size (); i++) printf ("%d%c", Q[i], (i== (Q.size ()-1))?'\ n':' '); - $}
Choosing capital for Treeland Codeforce 219-d