###### Codeforces Round #135 (Div. 2)-D. Choosing 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
3
2 1
2 3

output
0
2

input
4
1 4
2 4
3 4

output
2
1 2 3


#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;

struct Node{
Node(){
}
Node(int a, int b, int c){
from = a;
to = b;
v = c;
}
int from, to;
int v;
};
vector<Node> v[maxn];
int d[maxn];
int mins;
void dfs1(int k, int f){

for(int i = 0; i < v[k].size(); i++){
Node &e = v[k][i];
if(e.to != f){
d[1] += e.v;
dfs1(e.to, k);
}
}
}
void dfs2(int k, int f, int p){

for(int i = 0; i < v[k].size(); i++){
Node &e = v[k][i];
int c = p;
if(e.to != f){
if(e.v)
c--;
else
c++;
d[e.to] = d[1] + c;
mins = min(mins, d[e.to]);
dfs2(e.to, k, c);
}
}
}
int main(){

//	freopen("in.txt", "r", stdin);
int n, a, b;

scanf("%d", &n);
for(int i = 0; i < n-1; i++){
scanf("%d%d", &a, &b);
v[a].push_back(Node(a, b, 0));
v[b].push_back(Node(b, a, 1));
}
dfs1(1, -1);
mins = d[1];
dfs2(1, -1, 0);
printf("%d\n", mins);
int first = 0;
for(int i = 1; i <= n; i++){
if(d[i] != mins)
continue;
if(first == 0){
printf("%d", i);
first = 1;
}
else{
printf(" %d",i);
}
}
puts("");
return 0;
}

#### CodeForces D. Choosing Capital for Treeland （转化为树形DP）

2016-08-25 17:06:37

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

2015-09-23 10:28:23

#### CF 219D Choosing Capital for Treeland （树形dp）

2016-08-16 10:41:18

#### Codeforces Round #444 (Div. 2)-贪心&尺取-Ratings and Reality Shows

2017-11-11 20:11:11

#### Codeforces Round #446 (Div. 2) D. Gluttony

2017-11-18 21:07:25

#### Codeforces Round #426 (Div. 2) D. The Bakery（DP+线段树）

2017-07-31 09:16:44

#### CF-Codeforces Round #483 (Div. 2)-D-XOR-pyramid

2018-05-16 01:15:04

#### 【map离散&容斥】Ghosts @Codeforces Round #478 (Div. 2) D

2018-05-02 12:54:06

#### Educational Codeforces Round 36 (Rated for Div. 2) 题解

2018-01-13 23:16:24

#### Codeforces Round #471 (Div. 2)

2018-03-27 23:23:41

## 不良信息举报

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