【codeforce 219D】 Choosing Capital for Treeland (树形DP)

Description

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

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

Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.

Input

The first input line contains integer n (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 si, ti (1 ≤ si, ti ≤ nsi ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.

Output

In the first line print the minimum number of roads to be 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.

Sample Input

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

【题意】

  给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。

 

【分析】

  啊啊啊看题意的时候不小心看到题解了。

  啊啊啊我是不是很傻没有1秒钟看出来,数学老师说要一秒钟看出来的啊。

  嗯。。先随便找一个点作为根,计算它的值,具体怎么计算呢,就是不指向它的边要加1。(一个dfs搞定)

  然后从这个根往下走,往下走一步其实只有他们中间的边要方向,判断一下就好了。

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define Maxn 200010
 8 #define INF 0xfffffff
 9 
10 struct node
11 {
12     int x,y,c,next;
13 }t[2*Maxn];int len=0;
14 int first[Maxn];
15 
16 void ins(int x,int y,int c)
17 {
18     t[++len].x=x;t[len].y=y;t[len].c=c;
19     t[len].next=first[x];first[x]=len;
20 }
21 
22 int ans=INF;
23 int op[Maxn],pl=0;
24 int d[Maxn];
25 
26 void dfs(int x,int f)
27 {
28     d[x]=0;
29     for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
30     {
31         int y=t[i].y;
32         dfs(y,x);
33         d[x]+=d[y]+t[i].c;
34     }
35 }
36 
37 void ffind(int x,int f)
38 {
39     if(d[x]<ans)
40     {
41         pl=0;
42         op[++pl]=x;ans=d[x];
43     }
44     else if(d[x]==ans) op[++pl]=x;
45     for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
46     {
47         int y=t[i].y;
48         if(t[i].c==1) d[y]=d[x]-1;
49         else d[y]=d[x]+1;
50         ffind(y,x);
51     }
52 }
53 
54 int main()
55 {
56     int n;
57     scanf("%d",&n);
58     for(int i=1;i<n;i++)
59     {
60         int x,y;
61         scanf("%d%d",&x,&y);
62         ins(x,y,0);ins(y,x,1);
63     }
64     dfs(1,0);
65     ffind(1,0);
66     sort(op+1,op+1+pl);
67     printf("%d\n",ans);
68     for(int i=1;i<=pl;i++) printf("%d ",op[i]);
69     printf("\n");
70     return 0;
71 }
[CF 219D]

 

 

2016-10-17 15:04:38

 

posted @ 2016-10-17 15:00  konjak魔芋  阅读(254)  评论(0编辑  收藏  举报