• 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


  • 相关阅读:
    char 型变量中能不能存贮一个中文汉字,为什么?
    抽象类(abstract class)和接口(interface)有什么异同?
    描述一下JVM加载class文件的原理机制?
    重载(Overload)和重写(Override)的区别。重载的方法能否根据返回类型进行区分?
    String和StringBuilder、StringBuffer的区别?
    此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
    是否可以继承String类?
    两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
    laraval join 的理解
    whereHasIn方法
  • 原文地址:https://www.cnblogs.com/smallhester/p/10389740.html
Copyright © 2020-2023  润新知