Choosing Capital for Treeland codeforce 219-D

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

32 12 3

Output

02 

Input

41 42 43 4

Output

 21 2 3

题解:将方向转化为边的权值,正向边权值为0,反向边权值为1.以1为根DFS,得到1到其它所有点的总花费。重点是第二次DFS:

       

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn=2e5+5;
 5
 6 struct node{
 7     int to,next,va;
 8 }e[2*maxn];
 9
10 int n,tot;
11 int dp[maxn],head[maxn];
12
13 void Inite(){
14     tot=0;
15     memset(head,-1,sizeof(head));
16 }
17
18 void addedge(int u,int v,int w){
19     e[tot].to=v;
20     e[tot].va=w;
21     e[tot].next=head[u];
22     head[u]=tot++;
23 }
24
25 void DFS1(int pa,int u){
26     for(int i=head[u];i!=-1;i=e[i].next){
27         int v=e[i].to;
28         if(pa==v) continue;
29         DFS1(u,v);
30         dp[u]+=dp[v]+e[i].va;
31     }
32 }
33
34 void DFS2(int pa,int u){
35     for(int i=head[u];i!=-1;i=e[i].next){
36         int v=e[i].to;
37         if(pa==v) continue;
38         dp[v]+=(dp[u]-dp[v])+((e[i].va)?-1:1);
39         DFS2(u,v);
40     }
41 }
42
43 int main()
44 {   Inite();
45     memset(dp,0,sizeof(dp));
46
47     scanf("%d",&n);
48     for(int i=2;i<=n;i++){
49         int u,v;
50         scanf("%d%d",&u,&v);
51         addedge(u,v,0);
52         addedge(v,u,1);
53     }
54
55     DFS1(0,1);
56     DFS2(0,1);
57
58     int temp=1e9;
59     vector<int> q;
60     for(int i=1;i<=n;i++) if(dp[i]<temp) temp=dp[i];
61     for(int i=1;i<=n;i++) if(dp[i]==temp) q.push_back(i);
62     printf("%d\n",temp);
63     for(int i=0;i<q.size();i++) printf("%d%c",q[i],(i==(q.size()-1))?‘\n‘:‘ ‘);
64
65 }
				
时间: 2024-11-12 17:36:44

Choosing Capital for Treeland codeforce 219-D的相关文章

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

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

(纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

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

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

题目传送门 1 /* 2 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 3 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计算dp[v], 4 此时可以从上个节点的信息递推出来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using name

Choosing Capital for Treeland CodeForces - 219D (树形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

codeforces:219D. Choosing Capital for Treeland

题目大意:国家由n个城市以及n-1条连接不同城市的道路组成(每条道路都有正向和逆向之分),并且每个城市到另外一个城市都至少存在一条路径.现在议会要决定选一个城市作为首都.当一个城市选为首都时,需要将所有从首都到其它城市的路径上的所有边都是正向的(如果不是正向的则需要颠转道路).求这样的首都,使得需要颠转的道路数目最小. 其中2<=n<=2e5. 首先这显然是一副无向无环连通图(参考我的博客连通图的一些性质).因此从任意一个城市出发到另外一个城市都有唯一一条路径. 为了后面分析的简便,这里记选取

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

题目链接: http://codeforces.com/contest/219/problem/D 题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到u去,则要逆转该边方向)如果有多个这样的点,则升序输出所有 思路: 看了三篇博客,挺好的 http://blog.csdn.net/chl_3205/article/details/9284747 http://m.blog.csdn.net/qq_32570675/article/d

Codeforces 219D. Choosing Capital for Treeland (树dp)

题目链接:http://codeforces.com/contest/219/problem/D 树dp 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio&

CF 219D Choosing Capital for Treeland 树形DP 好题

一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次

【CF】135 Div2 Choosing Capital for Treeland

树形结构,挺有意思的题目.不难. 1 /* 219D */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorith