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

177 篇文章 0 订阅
77 篇文章 8 订阅
D. Choosing Capital for Treeland
time limit per test: 3 seconds
memory limit per test: 256 megabytes

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 


题目链接: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");
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值