• Choosing Capital for Treeland CodeForces


    传送门

    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 - 1lines 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,分别搜索一个点的子树的需要改变方向的个数(dfs1)和非子树需要改变方向的个数(dfs2)。
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<cstdlib>
    #include <vector>
    #include <set>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    #define PI acos(-1.0)
    const int maxn =  2e5+5;
    const ll mod = 1e9+7;
    
    typedef pair<int,int>edge;
    
    vector<int>G[maxn];
    set<edge>st;
    int dp[maxn][2];
    /*
    dp[i][0]该结点子树有多少条边要改,
    dp[i][1]该结点的父节点有多少条边要改
    */
    void dfs1(int u,int pre)
    {
        dp[u][0] = dp[u][1] = 0;
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i];
            if(v == pre)
                continue;
            dfs1(v,u);
            if(st.find(edge{u,v}) == st.end())//如果u不能到v则需要逆转边
                dp[u][0]++;
            dp[u][0] += dp[v][0];
        }
    }
    void dfs2(int u,int pre)
    {
        for(int i=0;i<G[u].size();i++)
        {
            int v = G[u][i];
            if(v == pre)
                continue;
            dp[v][1] = dp[u][0] - dp[v][0] + dp[u][1];//(dp[u][0]-dp[v][0])就是u的除v结点外的其他子树逆转边
            if(st.find(edge{u,v}) != st.end())
                dp[v][1]++;
            else
                dp[v][1]--;
            dfs2(v,u);
        }
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            st.insert(edge{a,b});
            G[a].push_back(b);
            G[b].push_back(a);
        }
        dfs1(1,0);
        dfs2(1,0);
        int mn = INF;
        for(int i=1;i<=n;i++)
            mn = min(mn,dp[i][0] + dp[i][1]);
        printf("%d
    ",mn);
        for(int i=1;i<=n;i++)
        {
            if(dp[i][0] + dp[i][1] == mn)
                printf("%d ",i);
        }
    }
    View Code


  • 相关阅读:
    转战物联网·基础篇06-深入理解MQTT协议之基本术语
    转战物联网·基础篇05-通俗理解MQTT协议的实现原理和异步方式
    转战物联网&#183;基础篇04-不可不知的进制关系与数据传输的本质
    转战物联网·基础篇03-从JSON数据到短指令谈思维的转变
    转战物联网·基础篇02-物联网中的角儿
    转战物联网·基础篇01-物联网之我见
    nRF24L01+组网方式及防撞(防冲突)机制的实战分享
    ElementUi中el-table分页效果
    原生无缝Banner轮播图
    详解立即执行函数(function(){}()),(function(){})()
  • 原文地址:https://www.cnblogs.com/smallhester/p/10389740.html
Copyright © 2020-2023  润新知