CF291D Choosing Capital for Treeland <树形DP>

Problem

Choosing Capital for Treeland

Time  limit:  3000  ms\mathrm{Time\;limit:\;3000\;ms}
Memory  limit:  262144  kB\mathrm{Memory\;limit:\;262144\;kB}

Description

The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n1n-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(2n2×105)n (2\le n\le 2\times 10^5) — the number of cities in Treeland. Next n1n-1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si,ti(1si,tin;siti)s_i,t_i (1\le s_i,t_i\le n; si\ne ti) — the numbers of cities, connected by that road. The ithi^{th} road is oriented from city sis_i to city tit_i. You can consider cities in Treeland indexed from 11 to nn.

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.

Example

Input #1

1
2
3
3
2 1
2 3

Output #1

1
2
0
2

Input #2

1
2
3
4
4
1 4
2 4
3 4

Output #2

1
2
2
1 2 3

标签:树形DP

Translation

给出一棵由有向边组成的树,要求选一个点使得最小化从它到其余所有点的路径中反向边的条数之和。

Solution

首先转化问题为:给出一棵由无向边组成的树,边有权值0011(00为正向,11为反向),要求找一个点使得最小化其到所有点的路径的权值和。特别地,计算某个点时,此点到其父结点的边的边权要异或11
第一次DFSDFS,定下每边权值,并DPDP出每个点的子树的总权值。
第二次DFSDFS,按DFSDFS序计算答案。对于每个点,ans=f[fa]+(c==1?1:1)ans=f[fa]+(c == 1 ? -1 : 1),其中fafa为其父结点,cc为其到父结点的边的边权。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
#define MAX_N 200000
using namespace std;
int n, f[MAX_N+5], ans = 1; vector <int> G[MAX_N+5], E[MAX_N+5];
void addedge(int u, int v, int c) {G[u].push_back(v), E[u].push_back(c);}
void DFS1(int u, int fa) {
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i], c = E[u][i];
if (v == fa) continue;
DFS1(v, u), f[u] += f[v]+c;
}
}
void DFS2(int u, int fa) {
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i], c = E[u][i];
if (v == fa) continue;
f[v] = f[u]+(c ? -1 : 1), DFS2(v, u);
if (f[u] < f[ans] || (f[u] == f[ans] && u < ans)) ans = u;
}
}
int main() {
scanf("%d", &n); for (int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), addedge(u, v, 0), addedge(v, u, 1);
DFS1(1, 0), DFS2(1, 0); printf("%d\n", f[ans]); for (int i = 1; i <= n; i++) if (f[i] == f[ans]) printf("%d ", i);
return 0;
}