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·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 ≤ n; si ≠ 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.
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.
3 2 1 2 3
0 2
4 1 4 2 4 3 4
2 1 2 3
题意:有一棵有n个节点的数,有n-1条有向边,每条边的方向可以被翻转,但是代价为1,问你应该选取哪些点为根,使从根到达所有点的代价最下。先输出最小代价,然后输出所有的可取点。
思路:
采用两次dfs,从每个点都可以到达与他相邻的点,只不过走的方向不同代价就不同所以我们可以将题目给出的正向边的权值赋值为0,而他的反向边赋值为1 (和我们之前学习网络流是一样,也有这样的处理方法)那么题目就转换成了求每个点都其他点的权值之和的最下值了
关于上面这个问题就可以用树型DP的思想了:先找出和直接点的关系,然后找出和父节点的关系
首先用dfs1()求出每个点到以他为根子树的所有的节点的权值之和
然后用dfs2()从上往下更新一遍即可:
若dis(u,v) == 0,那么更新dp[v] = dp[u] + 1; //u是v的父节点
否则更新dp[v] = dp[u] - 1;
d代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=200010;
int n,tot;
int head[maxn];
int vis[maxn];
int dp[maxn];
int index[maxn];
int minn;
struct Edge{
int next;//下一条边的编号
int to;//到达的点
int len;//这条边的长度
}edge[maxn*3];
void add(int u,int v,int len){ //加一条从u到v长度为len的单向边
edge[++tot].next = head[u];
edge[tot].to = v;
edge[tot].len=len;
head[u] = tot;
}
void dfs1(int now){
vis[now]=1;
for(int i=head[now];i!=0;i=edge[i].next){
int next=edge[i].to;
if(vis[next])continue;
dfs1(next);
dp[now]+=dp[next]+edge[i].len;
}
}
void dfs2(int now){
vis[now]=1;
for (int i = head[now]; i != 0; i = edge[i].next)
{
int next = edge[i].to;
if (vis[next])
continue;
if (edge[i].len == 0)
dp[next] = dp[now] + 1;
else
dp[next] = dp[now] - 1;
if (dp[next] < minn)
minn = dp[next];
dfs2(next);
}
}
int main(){
int u,v;
int n;
while(cin>>n){
tot=0;
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
memset(head,0,sizeof(head));
for(int i=1;i<=n-1;i++){
cin>>u>>v;
add(u,v,0);
add(v,u,1);
}
dfs1(1);
minn=dp[1];
memset(vis, 0, sizeof(vis));
dfs2(1);
cout << minn << endl;
int k = 0;
for (int i = 1; i <= n; i++)
{
if (dp[i] == minn)
index[++k] = i;
}
for (int i = 1; i < k; i++)
cout << index[i] << " ";
cout << index[k] << endl;
}
return 0;
}