CodeForces - 219D Choosing Capital for Treeland(树状dp)

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, 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.

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
3
2 1
2 3
Output
0
2
Input
4
1 4
2 4
3 4
Output
2
1 2 3

题意: n个结点树,接下来n-1行描述树,每行有a,b两个数代表从a到b有一条单向边。找出结点,使从这个结点到其他结点都联通的时候需要反转的边最少。
思路: dfs两次,第一次找出每个点的儿子到这个点翻转所需的代价。dp[i][0]=dp[son][0]+两个点之间的方向。从下到上dfs。
第二次加上从这个点父亲那个方向到这个点的代价,dp[i][1]=dp[fa][1]-两个点之间的方向。从上到下dfs。

重要的是想明白父亲结点的总代价和儿子结点的总代价之间的关系。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<stdio.h>
#include<functional>
#include<time.h>
#pragma warning(disable:6031)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const ll mode = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950;
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }

struct Edge 
{
	int next, val;
};

vector<Edge>V[maxn];
int dp[maxn][2];

void dfs1(int u, int pre)//第一次dfs,从下到上
{
	dp[u][0] = 0;
	for (int i = 0; i < V[u].size(); i++)
	{
		int son = V[u][i].next;
		if (son == pre)continue;
		dfs1(son, u);
		dp[u][0] += (dp[son][0] + V[u][i].val);
	}
}

void dfs2(int u, int pre, int c)//第二次dfs,从上到下
{
	if (u == pre) //如果是根结点,答案就是第一次dfs的答案
		dp[u][1] = dp[u][0];
	else //父亲结点和儿子结点的关系
	{
		if (V[pre][c].val == 1)
			dp[u][1] = dp[pre][1] - 1;
		else
			dp[u][1] = dp[pre][1] + 1;
	}

	for (int i = 0; i < V[u].size(); i++)
	{
		int son = V[u][i].next;
		if (son == pre)continue;
		dfs2(son, u, i);//i用来传递son和u的方向
	}
}

int main() 
{
	int n;
	while (cin >> n)
	{
		for (int i = 1; i <= n; i++)V[i].clear();
		for (int i = 1; i < n; i++)
		{
			int a, b;
			cin >> a >> b;
			Edge E;
			E.next = b; E.val = 0;
			V[a].push_back(E);
			E.next = a; E.val = 1;
			V[b].push_back(E);
		}
		dfs1(1, 1);
		dfs2(1, 1, 0);
		int minn = (1 << 30);
		for (int i = 1; i <= n; i++)
			minn = min(dp[i][1], minn);
		cout << minn << endl;
		for (int i = 1; i <= n; i++)
			if (dp[i][1] == minn)
				cout << i << " ";
		cout << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值