zoukankan      html  css  js  c++  java
  • (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    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
    /*
    纪念自己第一道完全自己想出来的树DP!!!!
    
    给n个城市,n-1单向道路,要从中选择一个首都,首都的要求是从首都能到达所有其他城市,如果这些单向路中的方向不合适,可以重修这条路;
    问选择一个最合适的首都,是的修路的次数最少;
    
    思路:建图时,将路标记正向为0,逆向为1,dp[u]表示以u为首都最少修的路,dfs出dp[1]然后就能得出一个状态转移方程dp[v]=dp[u]+(w?-1:1);
    再用一次dfs就可以了
    */
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    #define N 200010
    #define INF 0x3f3f3f3f
    using namespace std;
    struct node
    {
        int to,len;//下一个节点,长度
        node (int x,int y)
        {
            to=x;
            len=y;
        }
    };
    int n;
    vector<node> edge[N*2];
    int dp[N];
    int ans=0;
    int dfs1(int u,int p)
    {
        for(int i=0;i<edge[u].size();i++)
        {
            int v=edge[u][i].to;
            int w=edge[u][i].len;
            if(v==p) continue;
            //cout<<"u="<<u<<" "<<"v="<<v<<" "<<"w="<<w<<endl;
            ans+=w;
            dfs1(v,u);
        }
        return ans;
    }
    void dfs2(int u,int p)
    {
        for(int i=0;i<edge[u].size();i++)
        {
            int v=edge[u][i].to;
            int w=edge[u][i].len;
            if(v==p) continue;
            //cout<<"u="<<u<<" "<<"v="<<v<<" "<<"w="<<w<<endl;
            dp[v]=dp[u]+(w?-1:1);
            dfs2(v,u);
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d",&n)!=EOF)
        {
            memset(dp,0,sizeof dp);
            for(int i=0;i<=n;i++)
                edge[i].clear();
            int a,b;
            for(int i=1;i<=n-1;i++)
            {
                scanf("%d%d",&a,&b);
                edge[a].push_back(node(b,0));
                edge[b].push_back(node(a,1));//逆向走的话就得花费一次
            }
            int min=INF;
            ans=0;
            dp[1]=dfs1(1,-1);
            //cout<<"dp[1]="<<dp[1]<<endl;
            dfs2(1,-1);
            for(int i=1;i<=n;i++)
            {
                if(dp[i]<min)
                    min=dp[i];
            }    
            printf("%d
    ",min);
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                if(dp[i]==min)
                {
                    printf(flag?" %d":"%d",i);
                    flag=1;
                }
            }    
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    4G DTU在城市景观照明中的应用解决方案
    物联网在物业管理和智慧楼宇中的应用解决方案
    4G工业路由器等物联网设备在食品安全检测中的应用
    NB-IoT网络在农业和畜牧业中的物联网智能灌溉应用案例
    串口服务器等应用于污水处理厂的自动监控和控制管理
    插卡式双卡4G工业路由器在数控机床远程控制中的应用
    4G工业路由器在水电站远程监控中的应用案例
    4G工业路由器在供水系统和道路交通远程检测中的应用案例
    HDU 6188 Duizi and Shunzi 贪心
    HDU 6185 Covering 矩阵快速幂
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5803477.html
Copyright © 2011-2022 走看看