Codeforces 219D Choosing Capital for Treeland 树型DP

来源:互联网 发布:微信js sdk 分享 编辑:程序博客网 时间:2024/06/08 20:17

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 



树型DP。

一棵树,其中每条边都是有向的,问以哪些节点为起点遍历整棵树,所需要的反向边最少。


给边加上权值,反向权值为1,正向权值为0.

先dfs,递归求每个节点到它的所有儿子节点需要反向多少条边,把所有路的权值加起来即可。

这时,根节点到每个节点的反向边数已经求出。

再bfs。每个节点与它的根节点相比,反向情况仅仅相差它与根节点之间的这一条边。既然如此,慢慢向后推就是了。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=200005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; int down[maxn],sum[maxn],fa[maxn],head[maxn];bool visit[maxn];int num;vector<int> v[maxn]; struct Edge {int from,to,pre,dist;};Edge edge[maxn*2];void addedge(int from,int to) {edge[num]=(Edge){from,to,head[from],0};head[from]=num++;edge[num]=(Edge){to,from,head[to],1};head[to]=num++;}void dfs(int now) {down[now]=0;visit[now]=1;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {dfs(to);fa[to]=now;down[now]+=edge[i].dist+down[to];}}}int main() {//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);    int n,i,x,y;    num=0;    memset(head,-1,sizeof(head));    scanf("%d",&n);    for (i=1;i<n;i++) {    scanf("%d%d",&x,&y);    addedge(x,y);    }    mem0(visit);    dfs(1);    queue<int> q;    mem0(visit);    q.push(1);    visit[1]=1;    mem0(sum);    sum[1]=down[1];    v[sum[1]].push_back(1);    int ans=sum[1];    while (!q.empty()) {    int now=q.front();    q.pop();    if (now!=1) {    sum[now]+=down[now];    sum[now]+=sum[fa[now]]-down[now];    v[sum[now]].push_back(now);    ans=min(ans,sum[now]);    }    for (i=head[now];i!=-1;i=edge[i].pre) {    int to=edge[i].to;    if (!visit[to]) {    visit[to]=1;    q.push(to);    if (edge[i].dist==0) sum[to]=1;    else sum[to]=-1;    }    }    }    printf("%d\n",ans);    sort(v[ans].begin(),v[ans].end());    int size=v[ans].size();    for (i=0;i<size-1;i++) {    printf("%d ",v[ans][i]);    }    printf("%d\n",v[ans][size-1]);return 0;}




阅读全文
0 0