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 #406 (Div. 2):C. Berzerk(记忆化搜索解决博弈问题)

C. Berzerk time limit per test 4 seconds memory limit per test 256 megabytes input standard in...
  • Jaihk662
  • Jaihk662
  • 2017年03月24日 15:23
  • 801

【Codeforces Round 375 (Div 2) E】【欧拉回路Fleury算法 或网络流】One-Way Reform 每条边定向使得最多的点满足入度=出度

E. One-Way Reform time limit per test 2 seconds memory limit per test 256 megabytes ...
  • snowy_smile
  • snowy_smile
  • 2016年10月04日 10:20
  • 737

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland(从每个点出发最小需要修改几条边的方向可以到达其它所有点,输出最小改变几)

题意: 有n个点的树(n
  • acm_fighting
  • acm_fighting
  • 2016年11月03日 21:04
  • 144

CF 251D Two Sets 异或高斯消元找最优解

CF251D Two Sets 异或高斯消元找最优解
  • YxuanwKeith
  • YxuanwKeith
  • 2016年04月08日 20:46
  • 725

Codeforces Round #347 (Div. 2) D. Graph Coloring(最少需要选择多少个点,使得所有边的颜色相同)

D. Graph Coloring time limit per test 1 second memory limit per test 256 megabytes ...
  • acm_fighting
  • acm_fighting
  • 2016年04月26日 20:17
  • 349

Codeforces Round #382 (Div. 2) -- D. Taxes (数学 -- 哥德巴赫猜想, 唯一分解定理)

大体题意: 你有n元钱,你需要交税,交锐的价格为n 的最大因子(不等于n),为了缩小税钱,你可以拆成多个钱相加的形式,但是不能拆出1来,问你最少交多少钱? 思路: 肯定是全都是素数最好了,那么问...
  • aozil_yang
  • aozil_yang
  • 2016年11月29日 00:19
  • 439

数学推公式——较难——Codeforces Round #187 (Div. 2)

题目链接: http://codeforces.com/contest/315/problem/C C. Sereja and Contest time limi...
  • CS_liuqing
  • CS_liuqing
  • 2013年06月16日 15:34
  • 1998

Codeforces Round #244 (Div. 2)(强连通分量,后缀数组)

A. Police Recruits #include #include #include #include using namespace std; int n; int main() { ...
  • u010660276
  • u010660276
  • 2014年05月23日 20:08
  • 421

Codeforces Round #248 (Div. 2) B题 【数据结构:树状数组】

题目链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),其中有m(1 ≤ m ≤...
  • u013912596
  • u013912596
  • 2014年05月24日 18:18
  • 708

Codeforces Round #312 (Div. 2) (第三题是位运算,好题)

比赛:点击打开链接 558A - Lala Land and Apple Trees 分析:从0坐标分开,负半轴一个数组,正半轴一个数组,来记录果树的左边和数量,可以用结构体数组来存储数据,其...
  • loveyou11111111
  • loveyou11111111
  • 2015年08月31日 16:30
  • 156
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #135 (Div. 2)-D. Choosing Capital for Treeland
举报原因:
原因补充:

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