CF219C hoosing Capital for Treeland

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 

题目表面了这些路径会构成一棵树一样的东西,建边就是,正向的边是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
}

时间: 2024-10-23 02:35:21

CF219C hoosing Capital for Treeland的相关文章

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

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. Over

(纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

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

树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

题目传送门 1 /* 2 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 3 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计算dp[v], 4 此时可以从上个节点的信息递推出来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using name

Choosing Capital for Treeland codeforce 219-D

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

Choosing Capital for Treeland CodeForces - 219D (树形DP)

传送门 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

CF 219D Choosing Capital for Treeland 树形DP 好题

一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次

codeforces:219D. Choosing Capital for Treeland

题目大意:国家由n个城市以及n-1条连接不同城市的道路组成(每条道路都有正向和逆向之分),并且每个城市到另外一个城市都至少存在一条路径.现在议会要决定选一个城市作为首都.当一个城市选为首都时,需要将所有从首都到其它城市的路径上的所有边都是正向的(如果不是正向的则需要颠转道路).求这样的首都,使得需要颠转的道路数目最小. 其中2<=n<=2e5. 首先这显然是一副无向无环连通图(参考我的博客连通图的一些性质).因此从任意一个城市出发到另外一个城市都有唯一一条路径. 为了后面分析的简便,这里记选取

CodeForces 219D.Choosing Capital for Treeland (树形dp)

题目链接: http://codeforces.com/contest/219/problem/D 题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到u去,则要逆转该边方向)如果有多个这样的点,则升序输出所有 思路: 看了三篇博客,挺好的 http://blog.csdn.net/chl_3205/article/details/9284747 http://m.blog.csdn.net/qq_32570675/article/d

【CF】135 Div2 Choosing Capital for Treeland

树形结构,挺有意思的题目.不难. 1 /* 219D */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorith