Codeforces 219D Choosing Capital for Treeland【思维+树型Dp】

351 篇文章 2 订阅

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
3
2 1
2 3
output
0
2 
input
4
1 4
2 4
3 4
output
2
1 2 3 

题目大意:


给出具有N个节点的树,现在给出N-1条有向边,问我们最少改变多少条边的方向,使得我们能够从一个点,到达其余所有的点。并且输出各个点的编号。


思路:

①树上处理的最优解问题,我们很难不去想Dp,我们设定Dp【i】表示,以i为根节点,其到达子树中各个节点需要改变的边的个数。

那么不难写出其状态转移方程:Dp【u】+=Dp【v】+W(u,v),我们这里设定不改变方向的边权值为0,改变方向的反向边的权值为1即可。


②树型dp无异于两个方向,一个子树方向,一个非子树方向,那么再设定F【i】,表示以节点i为中心,到达非子树方向的各个节点的最小花费。

那么也不难写出其状态转移方程:

F【u】+=F【from】+W(u,from)【W(u,from)==1-W(from,u)】;

F【u】+=Dp【u】-Dp【v】-W(from,u);


过程维护一下输出最优解即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
    int from,to,next,w;
}e[1050000];
int cont;
int head[250000];
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
/*********************************/
int dp[250000];
int F[250000];
void Dfs(int u,int from)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to,w=e[i].w;
        if(v==from)continue;
        Dfs(v,u);
        dp[u]=dp[u]+w+dp[v];
    }
}
void dfs(int u,int from,int prew)
{
    if(from!=-1)F[u]=F[from]+1-prew;
    if(from!=-1)F[u]+=dp[from]-prew-dp[u];
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(v==from)continue;
        dfs(v,u,w);
    }
}
/*********************************/
vector<int>ans;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        cont=0;
        ans.clear();
        memset(dp,0,sizeof(dp));
        memset(F,0,sizeof(F));
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n-1;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            add(x,y,0);add(y,x,1);
        }
        Dfs(1,-1);
        dfs(1,-1,0);
        int minn=0x3f3f3f3f;
        for(int i=1;i<=n;i++)
        {
            if(dp[i]+F[i]<minn)
            {
                ans.clear();
                minn=dp[i]+F[i];
            }
            if(dp[i]+F[i]==minn)
            {
                ans.push_back(i);
            }
        }
        printf("%d\n",minn);
        for(int i=0;i<ans.size();i++)
        {
            int v=ans[i];printf("%d ",v);
        }
        printf("\n");
    }
}







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值