Codeforces 219D 树DP 解题报告

D. Choosing Capital for Treeland

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 ≤ n; si ≠ 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

【解题报告】
题目是给一张边有向的树形图。要选出中心点,从中心点要都能走到其他点,因此要反转一些边的方向。问可以选哪几个点作为首都,使它们所需反转边的数量最少。
其实不难想。首先就不妨设正向边权值为0,反向边权值为1,那样问题就是各个点出发到其他点经过边所需的最少权值和。
然后我们考虑从一个点的上面和下面来考虑DP。
dp0(u) 表示以 u 为中心点,它的子树需要反转的边数。显然dp0(u)+=dp0(v)+w(u,v)
dp1(u) 表示以 u 为中心点,它的向上部分需要反转的边数。显然dp1(u)=dp0(u)dp0(v)w(u,v)+dp1(u)+w(v,u)
那么一个点作为中心点所需要反转的边数就是 dp0(u)+dp1(u)
枚举更新答案答案就好了

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 200010
#define inf 0x3f3f3f3f

int n,cnt=-1,head[N];
struct Edge{int to,nxt,w;}e[N<<1];
int dp0[N],dp1[N];

void adde(int u,int v)
{
    e[++cnt].to=v;e[cnt].w=0;
    e[cnt].nxt=head[u];head[u]=cnt;
    e[++cnt].to=u;e[cnt].w=1;
    e[cnt].nxt=head[v];head[v]=cnt;
}
void dfs0(int u,int fa)
{
    for(int i=head[u];~i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa) continue;
        dfs0(v,u);
        dp0[u]+=dp0[v]+e[i].w;
    }
}
void dfs1(int u,int fa)
{
    for(int i=head[u];~i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa) continue;
        dp1[v]=dp0[u]-dp0[v]-e[i].w+dp1[u]+e[i^1].w;
        dfs1(v,u);
    }
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    for(int i=1;i<n;++i)
    {
        int u,v;scanf("%d%d",&u,&v);
        adde(u,v);
    }
    dfs0(1,1);dfs1(1,1);
    int ans=inf;
    for(int i=1;i<=n;++i) ans=min(ans,dp0[i]+dp1[i]);   
    printf("%d\n",ans);
    for(int i=1;i<=n;++i) if(ans==dp0[i]+dp1[i]) printf("%d ",i);
    puts("");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值