题目链接

Choosing Capital for Treeland

题目描述

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$ $\le$ $n$ $\le$ $2*10^5$) — 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 $s_i,t_i$ ($1$ $\le$ $s_i,t_i$ $\le$ $n$;$s_i$ $\neq$ $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题。
我们钦定一个根,dp点$i$子树下朝向和背向它的边和子树外朝向和背向它的边。
那么我们第一次dfs就可以求出子树下的两个dp,这个转移很简单,就不讲了。
第二次dfs我们就可以转移子树上的两个dp。

upin[u]=upin[fa];
upin[u]+=ssin[fa]-ssin[u]-(!kfa[u]);
upin[u]+=kfa[u];
upout[u]=upout[fa];
upout[u]+=sout[fa]-sout[u]-kfa[u];
upout[u]+=(!kfa[u]);

$upin[u]$表示$u$上面朝向$u$的边数。
$upout[u]$表示$u$上面背向$u$的边数。
$ssin[u]$表示$u$下面朝向$u$的边数。
$sout[u]$表示$u$下面背向$u$的边数。
$kfa[u]$表示$u$连向父亲的边的方向。
上代码:

#include<bits/stdc++.h>
using namespace std;
int n;
int u,v;
struct aa{
    int to,nxt;
    bool k;
}p[400009];
int h[200009],len=1;
void add(int u,int v,bool k){
    p[++len].to=v;
    p[len].k=k;
    p[len].nxt=h[u];
    h[u]=len;
}
bool kfa[200009];
int fa[200009];
int ssin[200009],sout[200009],upin[200009],upout[200009];
void dfs(int u,int fa){
    for(int j=h[u];j;j=p[j].nxt){
        if(p[j].to==fa) continue;
        dfs(p[j].to,u);
        ssin[u]+=ssin[p[j].to];
        sout[u]+=sout[p[j].to];
        kfa[p[j].to]=p[j].k;
        if(p[j].k) sout[u]++;
        else ssin[u]++;
    }
}
void dfss(int u,int fa){
    if(fa!=0){
        upin[u]=upin[fa];
        upin[u]+=ssin[fa]-ssin[u]-(!kfa[u]);
        upin[u]+=kfa[u];
        upout[u]=upout[fa];
        upout[u]+=sout[fa]-sout[u]-kfa[u];
        upout[u]+=(!kfa[u]);
    }
    for(int j=h[u];j;j=p[j].nxt){
        if(p[j].to==fa) continue;
        dfss(p[j].to,u);
    }
}
int main(){
    scanf("%d",&n);
    for(int j=1;j<n;j++){
        scanf("%d%d",&u,&v);
        add(u,v,1);
        add(v,u,0);
    }
    dfs(1,0);
    dfss(1,0);
    int mn=n;
    for(int j=1;j<=n;j++)
        mn=min(mn,ssin[j]+upin[j]);
    printf("%d\n",mn);
    for(int j=1;j<=n;j++)
        if(ssin[j]+upin[j]==mn) printf("%d ",j);
    return 0;
}