zoukankan      html  css  js  c++  java
  • CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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

    巧妙的转换,正向边权0,逆向边权1

    两遍dfs分别求f[i]到子节点的边权和 和 g[i]从i往上的边权和

    注意把ans=min(ans,f[u]+g[u]);写在dfs开始处,否则处理不了1最小的时候

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int N=2e5+5,INF=1e9+5;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,u,v;
    struct edge{
        int v,w,ne;
    }e[N<<1];
    int cnt=0,h[N];
    inline void ins(int u,int v){
        cnt++;
        e[cnt].v=v;e[cnt].w=0;e[cnt].ne=h[u];h[u]=cnt;
        cnt++;
        e[cnt].v=u;e[cnt].w=1;e[cnt].ne=h[v];h[v]=cnt;
    }
    int f[N];
    void dfs1(int u,int fa){
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(v==fa) continue;
            dfs1(v,u);
            f[u]+=f[v]+w;
        }
    }
    int g[N],ans=INF;
    void dfs2(int u,int fa){//printf("u %d %d %d
    ",u,f[u],g[u]);
        ans=min(ans,f[u]+g[u]);
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(v==fa) continue;
            g[v]=g[u]+f[u]-f[v]+(w==0?1:-1);
            dfs2(v,u);
        }
    }
    int main(){
        n=read();
        for(int i=1;i<=n-1;i++){u=read();v=read();ins(u,v);}
        dfs1(1,0);
        dfs2(1,0);
        printf("%d
    ",ans);
        for(int i=1;i<=n;i++) if(g[i]+f[i]==ans) printf("%d ",i);
    }
  • 相关阅读:
    Codeforces Round #398 (Div. 2) B,C
    KMP模板
    HDU1711 KMP(模板题)
    HDU3265 线段树(扫描线)
    HDU2795 线段树
    HDU1828线段树(扫描线)
    HDU1832 二维线段树求最值(模板)
    HDU1698 线段树(区间更新区间查询)
    HDU3251 最大流(最小割)
    cf2.c
  • 原文地址:https://www.cnblogs.com/candy99/p/6072322.html
Copyright © 2011-2022 走看看