CF 219D 树形dp

来源:互联网 发布:单片机应用技术 编辑:程序博客网 时间:2024/05/01 07:51
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 withunidirectional 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 citya 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. Nextn - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integerssi, ti (1 ≤ si, ti ≤ nsi ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from citysi to cityti. You can consider cities in Treeland indexed from 1 ton.

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 test(s)
Input
32 12 3
Output
02 
Input
41 42 43 4
Output
21 2 3 


题意:一棵n个点的树,有n-1条有向边,求改变最少的边的方向,使得一个点可以遍历到所有的其他n-1个点。

将有向边正向权值为0,反向权值为1建立无向图,则问题转化为从每一个点到其他所有点的权值和,然后求从1点开始向下遍历,求从1点开始到达其他所有点的权值,

然后第二次dfs,从1点从下递推,可以得到每个点到其他n-1个点的权值,然后就得到了答案。

代码:

/* ***********************************************Author :xianxingwuguanCreated Time :2014-2-11 2:29:40File Name :4.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;struct NODE{      int next,to,val;}edge[500000];int head[200010],tol,sum[210000];void add(int u,int v,int w){      edge[tol].to=v;      edge[tol].next=head[u];      edge[tol].val=w;      head[u]=tol++;}void dfs(int u,int pre){      for(int i=head[u];i!=-1;i=edge[i].next){            int v=edge[i].to;            if(v==pre)continue;            if(edge[i].val)sum[u]++;            dfs(v,u);            sum[u]+=sum[v];      }}void dfs1(int u,int pre){      for(int i=head[u];i!=-1;i=edge[i].next){            int v=edge[i].to;            if(v==pre)continue;            if(edge[i].val)sum[v]=sum[u]-1;            else sum[v]=sum[u]+1;            dfs1(v,u);      }}int main(){      int i,j,k,m,n;      while(~scanf("%d",&n)){            memset(head,-1,sizeof(head));            tol=0;m=n-1;            while(m--){                  scanf("%d%d",&i,&j);                  add(i,j,0);                  add(j,i,1);            }            memset(sum,0,sizeof(sum));            dfs(1,0);            dfs1(1,0);            int ans=n;            for(i=1;i<=n;i++)ans=min(ans,sum[i]);            cout<<ans<<endl;for(i=1;i<=n;i++)if(sum[i]==ans)cout<<i<<" ";cout<<endl;      }      return 0;}


0 0
原创粉丝点击