CodeForces

来源:互联网 发布:joker game动漫 知乎 编辑:程序博客网 时间:2024/06/12 20:44
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 ≤ 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.

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

32 12 3Output02 Input41 42 43 4Output21 2 3 

题意:给你一颗有向的树(或者可以理解为一个有向无环图),问你从任意一个节点出发,到其他所有节点要逆方向走多少条路?

思路:(我是按照一棵树写的)因为这棵树有方向的,那么对于任意节点需要有向上和向下两个方向,两遍dfs,第一遍向下处理(搜出节点向下需要的逆方向路数),第二遍从上向下更新出所有逆方向路数。状态方程:
dp[fa][0] += dp[child][0] + direction(fa,child)的真假;
dp[child][1] = dp[fa][1] + direction(fa,child)的方向;

*写这类题,画好图,构造树形结构,找(fa, child)的关系。通常都会用2~3个dp二维数组递推,向上和向下两个方向:
1. 向下遍历,向上更新(dp在dfs后)。
2. 向下遍历,向下更新(dp在dfs前)。

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <queue>using namespace std;const int MAXN = 2 * 1e5 + 5;int dp[MAXN][5];int n, p1, p2;struct node {    int to;    int dir; //标记方向 };vector<node> v[MAXN];void dfs1(int x, int fa) { //向下遍历,向上更新    for(int i = 0; i < v[x].size(); i++) {        node e = v[x][i];        if(e.to == fa) continue;        dfs1(e.to, x);        if(e.dir == 1) dp[x][0] += dp[e.to][0];        else dp[x][0] += dp[e.to][0] + 1;    }}void dfs2(int x, int fa) { //向下遍历,向下更新    for(int i = 0; i < v[x].size(); i++) {        node e = v[x][i];        if(e.to == fa) continue;        if(e.dir == 1) dp[e.to][1] = dp[x][1] + 1;        else dp[e.to][1] = dp[x][1] - 1;        dfs2(e.to, x);    }}void solve() {    int ans = 1e9;    for(int i = 1; i <= n; i++) {        ans = min(ans, dp[i][1]);    }    printf("%d\n", ans);    bool flag = false;    for(int i = 1; i <= n; i++) {        if(dp[i][1] == ans) {            if(flag) printf(" ");            printf("%d", i);            flag = true;        }    }    printf("\n");}int main() {    scanf("%d", &n);    for(int i = 1; i < n; i++) {        node e;        scanf("%d %d", &p1, &p2);        e.to = p2;        e.dir = 1;         v[p1].push_back(e);        e.to = p1;        e.dir = -1;        v[p2].push_back(e);    }    dfs1(1, -1);    dp[1][1] += dp[1][0];    dfs2(1, -1);//  for(int i = 1; i <= n; i++) {//      printf("%d -> %d -> %d\n", i, dp[i][0], dp[i][1]);//  }    solve();    return 0;}
原创粉丝点击