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.
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.
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.
3
2 1
2 3
0
2
4
1 4
2 4
3 4
2
1 2 3
题目链接:http://codeforces.com/problemset/problem/219/D
题目大意:给一棵树,边是有向边,求最少改变几条边的方向,可以使某个点可以到达其他所有点
题目分析:两次DFS,将点1作为树根,第一次求出从根到其他所有点最少需要改变的次数,建树的时候给正向边赋值0,反向边赋值1,第二次DFS时根据第一次的结果得到每个点到其他所有点分别需要的最少改变次数,相当于将其他每个点当做根,这样它们作为子树根时遍历整个子树的最小值在第一次DFS时已经得到,还需要考虑其余部分,其实可以发现当前结点的父亲到其他所有点所要改变的最小值也已算出,因此只需要看其和其父亲之间边的关系,如果是正向边则需要变成反向边即加1,反向边则要减1,因为原来从父亲到自己时对反向边变了一次向多加了1。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x7fffffff;
int const MAX = 2e5 + 5;
struct EDGE {
int to, nxt, val;
}e[MAX << 1];
int n, dp[MAX];
int head[MAX], cnt;
int ans[MAX], sum;
void Init() {
cnt = 0;
memset(dp, 0, sizeof(dp));
memset(head, -1, sizeof(head));
}
void Add(int u, int v) {
e[cnt].to = v;
e[cnt].val = 0;
e[cnt].nxt = head[u];
head[u] = cnt ++;
e[cnt].to = u;
e[cnt].val = 1;
e[cnt].nxt = head[v];
head[v] = cnt ++;
}
void DFS1(int u, int fa) {
for (int i = head[u]; i != -1; i = e[i].nxt) {
int v = e[i].to;
int val = e[i].val;
if (v != fa) {
DFS1(v, u);
dp[u] += dp[v] + val;
}
}
}
void DFS2(int u, int fa) {
for (int i = head[u]; i != -1; i = e[i].nxt) {
int v = e[i].to;
int val = e[i].val;
if (v != fa) {
dp[v] += (dp[u] - dp[v]) + (val ? -1 : 1);
DFS2(v, u);
}
}
}
int main() {
Init();
scanf("%d", &n);
int s, t;
for (int i = 1; i < n; i ++) {
scanf("%d %d", &s, &t);
Add(s, t);
}
DFS1(1, -1);
DFS2(1, -1);
sum = INF;
int num = 0;
for (int i = 1; i <= n; i ++) {
if (dp[i] < sum) {
sum = dp[i];
memset(ans, 0, sizeof(ans));
num = 0;
ans[num ++] = i;
}
else if (dp[i] == sum) {
ans[num ++] = i;
}
}
printf("%d\n", sum);
for (int i = 0; i < num; i ++) {
printf("%d ", ans[i]);
}
printf("\n");
}