CF219C hoosing Capital for Treeland

解决Treeland中如何选择首都以最小化道路翻转次数的问题,采用树形DP算法,通过一次DFS预处理得到一个点所需的翻转次数,再通过递归更新其他节点的最优解。
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

 题目表面了这些路径会构成一棵树一样的东西,建边就是,正向的边是0,反向是1,先DFS暴力求出一个点所需翻转的次数,然后其他点就可以根据这个点来求,因为当你知道某个点的答案时,那与它相连的点的答案你也知道,ans[u]=ans[v]+(u->v==0?1:-1),画图就能知道这个了,做完查了下别人的题解,发现这个叫树形dp。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
typedef long long ll;
#define X first
#define Y second
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define sd(x) scanf("%d",&(x))
#define Pi acos(-1.0)
#define sf(x) scanf("%lf",&(x))
#define ss(x) scanf("%s",(x))
#define maxn 10000000
#include <ctime>
const int inf=0x3f3f3f3f;
const long long mod=1000000007;
using namespace std;
vector< pair<int,int> >g[200005];
int ans[200005];
int mn=inf;
int dfs(int e,int s)
{
    int ans=0;
    for(int i=0;i<g[e].size();i++)
    {
        int ne=g[e][i].X;
        if(ne!=s)
        {
            ans+=dfs(ne,e)+g[e][i].Y;
        }
    }
    return ans;
}
void solve(int e,int s)
{
    for(int i=0;i<g[e].size();i++)
    {
        int ne=g[e][i].X;
        if(ne!=s)
        {
            ans[ne]=ans[e]+(g[e][i].Y==0?1:-1);
            solve(ne,e);
        }
    }
    mn=min(mn,ans[e]);
}
int main()
{
    #ifdef local
    freopen("in","r",stdin);
    //freopen("data.txt","w",stdout);
    int _time=clock();
    #endif
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int s,e;
        scanf("%d%d",&s,&e);
        g[s].pb(mp(e,0));
        g[e].pb(mp(s,1));
    }
    ans[1]=dfs(1,0);
    solve(1,0);
    cout<<mn<<endl;
    for(int i=1;i<=n;i++)
    {
        if(ans[i]==mn)
            printf("%d ",i);
    }
    #ifdef local
    printf("time: %d\n",int(clock()-_time));
    #endif
}
View Code

 

转载于:https://www.cnblogs.com/scau-zk/p/5654237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值