Choosing Capital for Treeland (树形dp)

在不考虑道路方向的国家Treeland中,有n个城市通过n-1条道路连接,可以从任意城市到达另一城市。长老会需要选择一个首都,并确保所有道路定向使从首都出发能到达任何城市,问最少需要反转多少条道路。任务是找出使道路反转数最小的首都。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example
Input
3
2 1
2 3
Output
0
2 
Input
4
1 4
2 4
3 4
Output
2
1 2 3 


题目大概:

在一颗树中,边都是单项的,找到一个节点,使得它如果能到达任何一个结点的话,反转的边最少。

思路:

可以先把结点能去的结点dfs统计一下。

然后再dfs一遍,能去的地方把点减去,不能去的地方再加上1,最后的答案就是需要反转的数量。

代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
const int f=1000005;
int n,m;
int dp[200005][2];
int head[200005];
int minshu;
int ans;
struct shu
{
    int v;
    int next;
    int va;
}tr[400010];
void add(int q,int w,int v)
{
    tr[ans].v=w;
    tr[ans].va=v;
    tr[ans].next=head[q];
    head[q]=ans++;

}

void down(int x,int pa)
{
    dp[x][0]=0;
     for(int i=head[x];i!=-1;i=tr[i].next)
    {
        int son=tr[i].v;
        if(son!=pa)
        {
            int w=tr[i].va;
            down(son,x);
            dp[x][0]+=dp[son][0]+w;
        }
    }
}

void dfs(int x,int pa,int v)
{
    if(x==pa)
    {
        dp[x][1]=dp[x][0];
    }
    else
    {
        if(v==0)dp[x][1]=dp[pa][1]+1;
        else dp[x][1]=dp[pa][1]-1;
    }


    for(int i=head[x];i!=-1;i=tr[i].next)
    {
        int son=tr[i].v;
        if(son!=pa)
        {
            int w=tr[i].va;
            dfs(son,x,w);

        }
    }


}
int main()
{

    while(~scanf("%d",&n))
    {
        memset(head,-1,sizeof(head));
        ans=0;
        for(int i=1;i<n;i++)
        {
            int q,w;
            scanf("%d%d",&q,&w);
            add(q,w,0);
            add(w,q,1);
        }
        down(1,-1);
        dfs(1,1,0);
        int ans=f;
        for(int i=1;i<=n;i++)ans=min(dp[i][1],ans);
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)
        {
            if(dp[i][1]==ans)
            {
                printf("%d ",i);
            }
        }

    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值