【题解】codeforces 219D Choosing Capital for Treeland 树型dp

题目描述

Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。

输入输出格式

输入格式

输入包含多个测试点。对于每个测试点,每个测试点的第一行为一个正整数n(2<=n<=2e5)。接下来n-1行,每行两个正整数ai,bi,表示城市a到城市b有一条单向通行的道路。输入以空格分隔,以EOF结尾。

输出格式

对于每个测试点,第一行输出k,第二行升序输出所有满足条件的首都的编号。

题目描述

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.

输入输出格式

输入格式:

The first input line contains integer $n$(  $2<=n<=2·10^{5}$ ) — the number of cities in Treeland. Next n−1 n-1 n−1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers $s_{i}$,$t_{i}$​ ( $1<=s_{i}$,$t_{i}<=n$; $s_{i}≠t_{i}$ ) — the numbers of cities, connected by that road. The $i$-th road is oriented from city $s_{i}$ to city $t_{i}$ . You can consider cities in Treeland indexed from 1 to $n$ .

输出格式:

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.

输入输出样例

输入样例#1: 复制

3
2 1
2 3

输出样例#1: 复制

0
2

输入样例#2: 复制

4
1 4
2 4
3 4

输出样例#2: 复制

2
1 2 3

思路

树型dp+两遍dfs

  • 第一次自底向上的dfs中,我们用$dp[u]$表示以u节点为首都时,u到其子树所有节点S需要逆转的边数
  • 令正向边权值为0,反向边权值为1 得转移方程 $dp[u]=∑(dp[s]+w[i,s])$
  • 第二次 $dp[i]$的定义变成了u到全树节点需要逆转的边数

分情况讨论

设 f为u的父亲

  1. 当f->u这条边是正向的时候,以u为首都则需要将这条边逆转, $dp[u]+=dp[f]+1$
  2. 当f->u这条边是反向的时候,以u为首都不需要将这条边逆转,但由于u是f的子节点,因此在dp[f]中将这条边逆转了,所以 $dp[u]+=dp[f]-1$

代码

#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
inline int read(){
	int x=0,w=1;
	char ch=getchar();
	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
	if(ch=='-') w=-1,ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
	return x*w;
}
struct edge {
    int to,next;
    bool w;
}e[400010];
int head[200010],tot,dp[200010];
inline void add(int from,int to,bool w) {
    e[++tot]=(edge){to,head[from],w};
    head[from]=tot;
}
void dfs1(int u,int fa) {				//求u到子节点需要逆转的边数
    for(int i=head[u];i;i=e[i].next) {
    	int v=e[i].to;
        if(v==fa) continue;
        dfs1(v,u);
        dp[u]+=dp[v]+e[i].w;
    }
}
void dfs2(int u,int fa) {				//求u到全树需要逆转的边数
    for (int i=head[u];i;i=e[i].next) {
    	int v=e[i].to;
        if (v==fa) continue;
        dp[v]=dp[u]+(e[i].w?-1:1);
        dfs2(v,u);
    }
}
int main() {
    int n,a,b;
    while (scanf("%d",&n)!=EOF) {
        tot=0;
        memset(head,0,sizeof(head));
        for (int i=1;i<=n-1;i++) {
            a=read(),b=read();
            add(a,b,0);
            add(b,a,1);
        }
        memset(dp,0,sizeof(dp));
        dfs1(1,-1);dfs2(1,-1);
        int Min=99999999;
        for (int i=1;i<=n;i++) if (Min>dp[i]) Min=dp[i];
        printf("%d\n",Min);
        for (int i=1;i<=n;i++) if (Min==dp[i]) printf("%d ",i);
        printf("\n");
    }
    return 0;
}

 

posted @ 2018-05-14 16:50  bbqub  阅读(377)  评论(0编辑  收藏  举报