zoukankan      html  css  js  c++  java
  • [codeforces 219D] Choosing Capital for Treeland (树形dp)

    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.

    Examples
    input
    3
    2 1
    2 3
    output
    0
    2
    input
    4
    1 4
    2 4
    3 4
    output
    2
    1 2 3

    建图是给那些已给出的边(权值为0)建一条反边(权值为1)
    先一个dfs由下而上求出所有点到一个根(1)需要翻转的边数。
    之后再一个dfs由上而下求出每个点的ans值即可

    code;

    //By Menteur_Hxy
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    
    const int MAX=200010;
    const int INF=0x3f3f3f3f;
    int n, minn = INF, cnt;
    int head[MAX], dp[MAX];
    
    struct edges {
        int to, next, dis;
    }edge[MAX << 1];
    
    void add(int x, int y) {
        edge[++cnt].next = head[x];
        edge[cnt].to = y;
        head[x] = cnt;
        edge[++cnt].next = head[y];
        edge[cnt].to = x;
        edge[cnt].dis = 1;
        head[y] = cnt;
    }
    
    void dfs0(int u, int pre) {
        for(int i = head[u]; i; i = edge[i].next) {
            int v = edge[i].to;
            if(v != pre) {
                dfs0(v , u);
                dp[u] += dp[v] + edge[i].dis;
            }
        }
    }
    
    void dfs1(int u, int pre) {
        if(dp[u] <= minn) minn = dp[u];//更改后的具体见下
        for(int i = head[u]; i; i = edge[i].next) {
            int v = edge[i].to, w = edge[i].dis;
            if(v != pre) {
                dp[v] = dp[u] + (w? -1: 1);
                //if(dp[v] <= minn) minn = dp[v];
                /*一开始是像上面这样写的,
                    交了才发现这样会忽略掉最初的根节点QAQ作死成功
                        (即第一次dfs根节点 1 的值就是最小反转)*/
                dfs1(v, u);
            }
        }
    }
    
    int main() {
        scanf("%d",&n);
        for (int i = 1; i < n; i++) {
            int a, b;
            scanf("%d %d", &a, &b);
            add(a, b);
        }
        dfs0(1, -1);
        dfs1(1, -1);
        printf("%d
    ", minn);
        for(int i = 1; i <= n; i++) 
            if(dp[i] == minn) printf("%d ",i);
        return 0;
    }
    
    版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 装箱问题
    Java实现 蓝桥杯VIP 算法训练 入学考试
    Qt4.6.2已编译二进制版本在VS2005中的问题
    函数可重入问题reentrant functions(函数执行过程中可以被中断,允许多个副本)
    QT的Paint 系统
    取clientdataset detal中的 更新数据, 将detal 转 数据库脚本sql
    delphi 八字排盘源码(post数据以后,又分析数据)
    Working with Entity Relations in OData
    图片轮播插件Nivo Slider
    跨域访问 REST API
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9248001.html
Copyright © 2011-2022 走看看