码迷,mamicode.com
首页 > Windows程序 > 详细

Choosing Capital for Treeland CodeForces - 219D (树形DP)

时间:2019-02-17 01:04:02      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:script   选中   hose   i++   \n   iostream   --   bsp   osi   

传送门

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];
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())
            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];
        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\n",mn);
    for(int i=1;i<=n;i++)
    {
        if(dp[i][0] + dp[i][1] == mn)
            printf("%d ",i);
    }
}
View Code

 



Choosing Capital for Treeland CodeForces - 219D (树形DP)

标签:script   选中   hose   i++   \n   iostream   --   bsp   osi   

原文地址:https://www.cnblogs.com/smallhester/p/10389740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!