CodeForces

来源:互联网 发布:淘宝女式大衣 编辑:程序博客网 时间:2024/06/05 07:58
D. Choosing Capital for Treeland
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
32 12 3
output
02 
input
41 42 43 4
output
21 2 3 



题意 : 给一个有向无环图,要找一个点能够到其他所有的点逆转的边最少

分析: 
一开始没什么思路,经过提醒知道 可以将 出边权值设为 1  入边权值设为 0
这样的话就可以树dp解决问题了~~

AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;const int maxn=3e5;int dpson[maxn],dpfather[maxn];struct Edge{int u,v,w,next;}edge[maxn<<1];int cnt,head[maxn<<1];void init(){cnt=0;memset(head,-1,sizeof(head));memset(dpson,0,sizeof(dpson));memset(dpfather,0,sizeof(dpfather));}void addedge(int u,int v,int w){edge[cnt].u=u;edge[cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;}void dfs1(int x,int pre){//printf("!~~ %d %d\n",x,pre);for(int i=head[x];i!=-1;i=edge[i].next){if(edge[i].v==pre) continue;dfs1(edge[i].v,x);dpson[x]=dpson[x]+edge[i].w+dpson[edge[i].v];//printf("!! x=%d  v=%d  dpson[x]=%d dpson[v]=%d w=%d\n",x,edge[i].v,dpson[x],dpson[edge[i].v],edge[i].w);}}void dfs2(int x,int pre){for(int i=head[x];i!=-1;i=edge[i].next){if(edge[i].v==pre) continue;int w=edge[i].w;dpfather[edge[i].v]= (w?0:1)+dpfather[x]+dpson[x]-dpson[edge[i].v]-w;dfs2(edge[i].v,x);}}struct Anss{int ans,i;}anss[maxn];bool com(Anss a,Anss b){return a.ans>b.ans||a.ans==b.ans&&a.i<b.i;}int main(){int n;while(scanf("%d",&n)==1){init();for(int i=1;i<n;i++){int a,b;;scanf("%d%d",&a,&b);addedge(a,b,1);addedge(b,a,0);}dfs1(1,-1);dfs2(1,-1);for(int i=0;i<n;i++){anss[i].ans=dpson[i+1]+dpfather[i+1];anss[i].i=i+1;}sort(anss,anss+n,com);int p=anss[0].ans;printf("%d\n",n-1-p);printf("%d",anss[0].i);for(int i=1;i<n;i++)if(p==anss[i].ans)printf(" %d",anss[i].i);printf("\n");}}