Codeforces Round #135 (Div. 2)-D. Choosing Capital for Treeland

原创 2016年08月30日 19:17:45

原题链接

D. Choosing Capital for Treeland
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ nsi ≠ 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 


对于题中给的城市及道路,如果不关心边的方向,则是一棵树。所以先从1出发深搜遍历所有的点,记录遍历所有点需要反转的道路的数目d[1].在从1出发深搜,搜到i点时计算1到i的路线长度为k1,反转了k2条道路,则从i出发遍历所有的点需要反转的道路的数目 = d[1] - k2  + (k1 - k2) = d[1] + k1 - 2 * k2;

#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int a, int b, int c){
		from = a;
		to = b;
		v = c;
	}
	int from, to;
	int v;
};
vector<Node> v[maxn];
int d[maxn];
int mins;
void dfs1(int k, int f){
	
	for(int i = 0; i < v[k].size(); i++){
		Node &e = v[k][i];
		if(e.to != f){
			d[1] += e.v;
			dfs1(e.to, k);
		}
	}
}
void dfs2(int k, int f, int p){
	
	
	for(int i = 0; i < v[k].size(); i++){
		Node &e = v[k][i];
		int c = p;
		if(e.to != f){
			if(e.v)
			 c--;
			else
			 c++;
		    d[e.to] = d[1] + c;
		    mins = min(mins, d[e.to]);
			dfs2(e.to, k, c);
		}
	}	
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, a, b;
	
	scanf("%d", &n);
	for(int i = 0; i < n-1; i++){
		scanf("%d%d", &a, &b);
		v[a].push_back(Node(a, b, 0));
		v[b].push_back(Node(b, a, 1));
	}
	dfs1(1, -1);
	mins = d[1];
	dfs2(1, -1, 0);
	printf("%d\n", mins);
	int first = 0;
	for(int i = 1; i <= n; i++){
		if(d[i] != mins)
		 continue;
		if(first == 0){
			printf("%d", i);
			first = 1;
		}
		else{
			printf(" %d",i);
		}
	}
	puts("");
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Round #135 (Div. 2) - D. Choosing Capital for Treeland(dfs / 树形DP)

题目: http://codeforces.com/problemset/problem/219/D 题意: 给出n个点,n-1条有向边的树,以一个点为中心,从这个点出发到其他各点需要更改的...

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

CodeForces 219D Choosing Capital for Treeland | 树形dp

题意: 有n个城市,它们之间有n-1条路,形成一棵树。

CodeForces D. Choosing Capital for Treeland (转化为树形DP)

恭喜一下自己……这题没看题解做出来的(虽然思路不小心瞄到了一眼) 题意:给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边,如果v要到u去,则要逆转该边方向)如果有多...

codeforces 219D Choosing Capital for Treeland (树形dp)

codeforces 219D Choosing Capital for Treeland (树形dp)

解题报告:CodeForces - 219D. Choosing Capital for Treeland 简单树型DP

D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes...

Codeforces 219D - Choosing Capital for Treeland(树形dp)

题意: 给出一棵树,找出这样的点:这个点通过反转边的反向达到其他各个点,并且反转次数最少。同时要输出最少的反转次数。 题解: 两次搜索。dp[i][2]  0表示第一次搜索这个点要到其他点需反转...

codeforces 219D D. Choosing Capital for Treeland(树形dp)

题目连接:codeforces 219D题目大意:给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。题目分析: ...

CF 219D Choosing Capital for Treeland 树形DP

首先我们可以将有向图转化为无向图,即正向边权值为0,反向边权值为1。那么接下来求某个点到剩下所有点的距离最小值。首先我们可以选择一个点为根,求出其到剩下所有点的距离。那么接下来选择其它点为跟的情况可以...

uestc oj 1857 Choosing Capital for Treeland

这个题目让我学会了一个道理,有时候不知道开多大的数组,往往使劲往大了开,于是乎出现了。。。。。is too large 这样的错误(其实开的数组连一半都用不完) 当出现这个情况的时候就可以使用C++...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #135 (Div. 2)-D. Choosing Capital for Treeland
举报原因:
原因补充:

(最多只允许输入30个字)