Choosing Capital for Treeland
time limit per test:3 seconds
memory limit per test:256 megabytes
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 ≤ n; si ≠ 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
3
2 1
2 3
Output
0
2
Input
4
1 4
2 4
3 4
Output
2
1 2 3
/*
2017/3/9
time: 218ms
题意:有一棵有n个节点的数,有n-1条有向边,每条边的方向可以被翻转,但是代价为1,问你应该选取哪些点为根,
使从根到达所有点的代价最下。先输出最小代价,然后输出所有的可取点。
分析:
从每个点都可以到达与他相邻的点,只不过走的方向不同代价就不同
所以我们可以将题目给出的正向边的权值赋值为0,而他的反向边赋值为1 (和我们之前学习网络流是一样,也有这样的处理方法)
那么题目就转换成了求每个点都其他点的权值之和的最下值了
关于上面这个问题就可以用树型DP的思想了:先找出和直接点的关系,然后找出和父节点的关系
首先用dfs_sub()求出每个点到以他为根子树的所有的节点的权值之和
然后用dfs_up()从上往下更新一遍即可:
若dis(u,v) == 0,那么更新dp[v] = dp[u] + 1; //u是v的父节点
否则更新dp[v] = dp[u] - 1;
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 200010;
struct edge
{
int v,len,next;
}edges[2*maxn];
int head[maxn],e,n;
int dp[maxn];
int ansmin;
int ansid[maxn];
void addedges(int u,int v,int len)
{
edges[e].v = v;
edges[e].len = len;
edges[e].next = head[u];
head[u] = e++;
}
void dfs_sub(int u,int fa)
{
dp[u] = 0;
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v = edges[i].v;
if(v == fa) continue;
dfs_sub(v,u);
dp[u] += edges[i].len + dp[v];
}
}
void dfs_up(int u,int fa)
{
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v = edges[i].v;
if(v == fa) continue;
if(edges[i].len == 0) dp[v] = dp[u] + 1;
else dp[v] = dp[u] - 1;
if(dp[v] < ansmin) ansmin = dp[v];
dfs_up(v,u);
}
}
int main()
{
//freopen("in.txt","r",stdin);
int u,v;
while(scanf("%d",&n)!=EOF)
{
e = 0;
memset(head,-1,sizeof(head));
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&u,&v);
addedges(u,v,0);
addedges(v,u,1);
}
dfs_sub(1,0);
ansmin = dp[1];
dfs_up(1,0);
int num = 0;
for(int i=1;i<=n;i++)
{
if(dp[i] == ansmin) ansid[num++] = i;
}
printf("%d\n%d",ansmin,ansid[0]);
for(int i=1;i<num;i++)
printf(" %d",ansid[i]);
printf("\n");
}
return 0;
}