您的位置:首页 > 其它

CF219C hoosing Capital for Treeland

2016-07-08 18:00 267 查看


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 ≤ 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




题目表面了这些路径会构成一棵树一样的东西,建边就是,正向的边是0,反向是1,先DFS暴力求出一个点所需翻转的次数,然后其他点就可以根据这个点来求,因为当你知道某个点的答案时,那与它相连的点的答案你也知道,ans[u]=ans[v]+(u->v==0?1:-1),画图就能知道这个了,做完查了下别人的题解,发现这个叫树形dp。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
typedef long long ll;
#define X first
#define Y second
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define sd(x) scanf("%d",&(x))
#define Pi acos(-1.0)
#define sf(x) scanf("%lf",&(x))
#define ss(x) scanf("%s",(x))
#define maxn 10000000
#include <ctime>
const int inf=0x3f3f3f3f;
const long long mod=1000000007;
using namespace std;
vector< pair<int,int> >g[200005];
int ans[200005];
int mn=inf;
int dfs(int e,int s)
{
int ans=0;
for(int i=0;i<g[e].size();i++)
{
int ne=g[e][i].X;
if(ne!=s)
{
ans+=dfs(ne,e)+g[e][i].Y;
}
}
return ans;
}
void solve(int e,int s)
{
for(int i=0;i<g[e].size();i++)
{
int ne=g[e][i].X;
if(ne!=s)
{
ans[ne]=ans[e]+(g[e][i].Y==0?1:-1);
solve(ne,e);
}
}
mn=min(mn,ans[e]);
}
int main()
{
#ifdef local
freopen("in","r",stdin);
//freopen("data.txt","w",stdout);
int _time=clock();
#endif
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int s,e;
scanf("%d%d",&s,&e);
g[s].pb(mp(e,0));
g[e].pb(mp(s,1));
}
ans[1]=dfs(1,0);
solve(1,0);
cout<<mn<<endl;
for(int i=1;i<=n;i++)
{
if(ans[i]==mn)
printf("%d ",i);
}
#ifdef local
printf("time: %d\n",int(clock()-_time));
#endif
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: