codeforces 219D - Choosing Capital for Treeland (树形dp)

18 篇文章 0 订阅
10 篇文章 0 订阅

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, tinsiti) — 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个点,m条有向边

让任意选一个点,作为首都,然后使得能经过所有的点让逆向边的个数最少,并且输出那些点

 

 

首先可以想到需要建一个0,1权值的双向边,正向为0,逆向为1,

然后我们可以通过一次dfs,把任意一个点通往下面子树的所有点的权值和求出来,设ans1,接着就还需要求每个点,他们的父亲节点子树的权值和,可以观察下面这个图,考虑怎么求任意点父亲的权值和,如果当前父亲节点是u,孩子节点是v,

可以得知他是由几部分组成的,粉色,绿色,和v~u连边的权值

父亲子树的权值和 ans2[v] = ans2[u] - (ans1[u] - ans1[v]) + num(num作为边权,方向相反就是1,通向就是-1),得到转移方程后,一个dfs就ok了 ,把ans1 和 ans2加起来就可以了

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) {
    return a < -eps ? -1 : a > eps;
}
inline int cmp(db a,db b) {
    return sign(a - b);
}
ll mul(ll a,ll b,ll c) {
    ll res = 1;
    while(b) {
        if(b & 1) res *= a,res %= c;
        a *= a,a %= c,b >>= 1;
    }
    return res;
}
ll phi(ll x) {
    ll res = x;
    for(ll i = 2; i * i <= x; i++) {
        if(x % i == 0) res = res / i * (i - 1);
        while(x % i == 0) x /= i;
    }
    if(x > 1) res = res / x  * (x - 1);
    return res;
}
int fa[maxn];
int Find(int x) {
    if(x != fa[x]) return fa[x] = Find(fa[x]);
    return fa[x];
}
ll c,n,k,m;
int a[maxn];
vector<P>v[maxn];
int num[maxn];
struct node {
    int num1,num2;
} g[maxn];
int dis[maxn];
int ans1[maxn];
int ans2[maxn];
void dfs(int x,int fa){
    for(auto d:v[x]){
        if(d.fi == fa) continue;
        dfs(d.fi,x);
        ans1[x] += ans1[d.fi] + d.se;
    }
}
void dfs1(int x,int fa){
    for(auto d:v[x]){
        if(d.fi == fa) continue;
        int num = 1;
        if(!d.second) num = -1;
        ans2[d.fi] = ans1[x] - ans1[d.fi] - num + ans2[x];
        dfs1(d.fi,x);
    }
}
vector<int>ans[maxn];
int main() {
    ios::sync_with_stdio(false);
    while(cin >> n){
        for(int i = 1;i < n;i++){
            int u,vv;
            cin >> u >> vv;
            v[u].push_back(P(vv,0));
            v[vv].push_back(P(u,1));
        }
        memset(ans1,0,sizeof(ans1));
        memset(ans2,0,sizeof(ans2));
        dfs(1,0);
        dfs1(1,0);
        for(int i = 1;i <= n;i++)
            ans1[i] += ans2[i],ans[ans1[i]].push_back(i);
        for(int i = 0;i <= n;i++){
            if(ans[i].size()){
                cout << i << endl;
                for(auto d:ans[i])
                    cout << d << " ";
                cout << endl;
                break;
            }
        }
    }
    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值