Codeforces 219D. Choosing Capital for Treeland 樹形dp

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

Copy

3
2 1
2 3

output

Copy

0
2 

input

Copy

4
1 4
2 4
3 4

output

Copy

2
1 2 3 

給一個樹每個樹枝都是有向的,問如果想要分別以這n個節點爲根節點到其他n-1個節點都能到達至少需要修改幾有向邊條邊 最後輸出最少需要修改幾條邊以及分別是些點。

這個我覺得就是比較簡單的換根法首先以1爲根節點求出這個點到其他點所需要修改的最小邊數,然後他子節點由他父親推出來就是看父子節點之間的道路關係瞭如果是子結點指向父節點,那麼父節點的數組在統計的時候已經把他計算進去了可是兒子節點是不需要統計的既要把他給減去,否則就把他給加上。

這裏被三目運算符運算優先級卡了

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
typedef long long LL;
using namespace std;
const LL MOD=1e9+7;
struct si{
    int v;
    bool dis;
};

vector<si>hea[201009];
bool f[200009];
int dp[210009];
void dfs(int son,int fa){
    dp[son]=0;
    for(int i=0;i<hea[son].size();i++){
        int v=hea[son][i].v;
        if(v==fa)continue;
        dfs(v,son);
        if(hea[son][i].dis)
            dp[son]+=dp[v]+1;
        else
            dp[son]+=dp[v];
    }
}
void dfs1(int p,int pa){
    for(int i=0;i<hea[p].size();i++){
        int v=hea[p][i].v;
        if(v==pa)
            continue;
        dp[v]=dp[p]+(hea[p][i].dis?-1:1);
        dfs1(v,p);
    }
}
int main()
{
    int n;

    while(cin>>n){
        int a,b;
        for(int i=1;i<=n;i++)
            hea[i].clear();
        for(int i=1;i<n;i++){
            cin>>a>>b;
            si l,r;
            l.v=a,l.dis=1;
            hea[b].push_back(l);
            r.v=b,r.dis=0;
            hea[a].push_back(r);
        }
        dfs(1,-1);
        dfs1(1,-1);
        int maxx=0x3f3f3f3f;
        for(int i=1;i<=n;i++){
            if(dp[i]<maxx)
                maxx=dp[i];
        }
        vector<int>oi;
        for(int i=1;i<=n;i++)
        {
            if(dp[i]==maxx)
                oi.push_back(i);
        }
        cout<<maxx<<endl;
        for(int i=0;i<oi.size();i++){
            cout<<oi[i]<<' ';
        }
        cout<<endl;
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章