codeforces219D. 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

Copy

3
2 1
2 3

output

Copy

0
2 

input

Copy

4
1 4
2 4
3 4

output

Copy

2
1 2 3 

 

一、原题地址

点我传送

 

二、大致题意

有n个城市,n-1条单向边。要选择一些中心城市 i,满足从 i 出发能到达所有的城市,因为是单向边所以原图可能无法达到要求,所以现在可以对一些边进行翻转。

询问的是最少需要翻转的边条数。以及可以被选作中心城市的编号。

 

三、思路

对于单向边原本的方向上我们建立一条费用为0的边,对于他的反向,我们建立的是费用为1的边。

以 1 为根节点,向下DFS跑一棵树,可以记录的是每个点 nx 到这个点 其下的所有点所需的费用,记为dp[ nx ]。

此时可知,dp[ 1 ],其实就是点1到达其他所有点所需的费用,那么对于与他相邻的点 i ,所需的费用就等于 =  ( i 其下所需的费用 + i 到其上节点的费用)

i 到其上节点的费用实际上就等于  =  (该点的父节点 pre 到达其他所有点的费用 - 点 i 到其下点的费用 )- 连接 i 和 pre 节点的边的影响。

这两个式子就表达为了:

dp[ nx ]=( dp[ pre ]-dp[ nx ]-precost )+( dp[ nx ] )+(precost==1?0:1)

再化简一下,就是:

dp[ nx ]=dp[ pre ]+(precost==1?-1:1)

一边跑一边更新最值即可。

 

四、代码

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = (1 << 30) - 1;
const long long int INF = 1e18;
typedef long long LL;



int n;
struct Edge
{
    int next,v,cost;
}e[400005];
int ind,head[200005],dp[200005];
vector<int>ans;
int minn;

void add(int from,int to,int cost)
{
	e[ind].next=head[from];
	e[ind].v=to;
	e[ind].cost=cost;
	head[from]=ind++;
}

void init()
{
    ind=0;
    for(int i=1;i<=n;i++)head[i]=-1,dp[i]=0;
}

void read()
{
    scanf("%d",&n);
    init();
    for(int i=1;i<=n-1;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        add(u,v,0);
        add(v,u,1);
    }
}

int DFS(int nx,int pre,int precost)
{
    for(int i=head[nx];i!=-1;i=e[i].next)
    {
        int to=e[i].v;
        int cost =e[i].cost;
        if(to!=pre)
        {
            dp[nx]+=DFS(to,nx,cost);
        }
    }
    return dp[nx]+precost;
}

void dfs(int nx,int pre,int precost)
{
    if(nx!=1)
    {
        //dp[nx]=(dp[pre]-dp[nx]-precost)+(dp[nx])+(precost==1?0:1);
        dp[nx]=dp[pre]+(precost==1?-1:1);
        if(dp[nx]<minn)
        {
            ans.clear();
            minn=dp[nx];
            ans.push_back(nx);
        }
        else if(dp[nx]==minn)
            ans.push_back(nx);
    }
    for(int i=head[nx];i!=-1;i=e[i].next)
    {
        int to =e[i].v;
        int cost =e[i].cost;
        if(to!=pre)
        {
            dfs(to,nx,cost);
        }
    }
}

void work()
{
    DFS(1,-1,0);
    minn=dp[1];
    ans.push_back(1);
    dfs(1,-1,0);
    printf("%d\n",minn);
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++)
    {
        printf("%d ",ans[i]);
    }
}

int main()
{
    read();
    work();
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值