【CodeForces219D】Choosing Capital for Treeland
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\)
輸出格式
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;
}