CodeForces 219D Choosing Capital for Treeland
来源:互联网 发布:gif添加文字软件 编辑:程序博客网 时间:2024/05/23 19:20
D. Choosing Capital for Treeland
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.
32 12 3
02
41 42 43 4
21 2 3
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <queue>#include <ctime>#include <cstdlib>#include <stack>#include <map>#include <set>#include <list>#define MP make_pair#define PB push_back#define INT_INF 0x3fffffff#define LL_INF 0x3fffffffffffffff#define EPS 1e-12#define MOD 1000000007#define PI 3.14159265358979323846#define N 200010#define E 400010using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef unsigned int Uint;typedef double DB;struct Edge{ int st,en,val,next;} edge[E];int head[N] , tot;int f[N];void add_edge(int st,int en,int val){ edge[tot].st=st; edge[tot].en=en; edge[tot].val=val; edge[tot].next=head[st]; head[st]=tot++;}void dfs1(int u,int pre){ f[u]=0; for(int e=head[u]; e!=-1; e=edge[e].next) { int v=edge[e].en; if(v==pre) continue; f[u]+=edge[e].val; dfs1(v,u); f[u]+=f[v]; }}vector<int> res;int ans;void dfs2(int u,int pre,int now){ if(now+f[u]<ans) { res.clear(); ans=now+f[u]; res.PB(u); } else if(now+f[u]==ans) res.PB(u); for(int e=head[u]; e!=-1; e=edge[e].next) { int v=edge[e].en; if(v==pre) continue; int cnt=now+f[u]-f[v]; if(edge[e].val==0) cnt++; if(edge[e].val==1) cnt--; dfs2(v,u,cnt); }}int main(){ int n; while(~scanf("%d",&n)) { memset(head,-1,sizeof(head)); tot=0; for(int i=1,a,b; i<n; i++) { scanf("%d%d",&a,&b); add_edge(a,b,0); add_edge(b,a,1); } dfs1(1,-1); ans=INT_INF; res.clear(); dfs2(1,-1,0); sort(res.begin(),res.end()); printf("%d\n",ans); for(int i=0; i<(int)res.size(); i++) printf("%d ",res[i]); printf("\n"); } return 0;}
- CodeForces 219D Choosing Capital for Treeland
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- CodeForces 219D Choosing Capital for Treeland (树形dp)
- CodeForces 219D Choosing Capital for Treeland | 树形dp
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
- codeforces 219D Choosing Capital for Treeland(树形DP)
- CodeForces 219D Choosing Capital for Treeland (树形DP)
- CodeForces 219D-H - Choosing Capital for Treeland-树DP
- CodeForces 219D Choosing Capital for Treeland(树形DP)
- codeforces 219D Choosing Capital for Treeland (树形dp)
- 【树形DP】codeforces 219D Choosing Capital for Treeland
- Codeforces 219D Choosing Capital for Treeland 树型DP
- Codeforces 219D Choosing Capital for Treeland DP(Tree)
- Codeforces 219D Choosing Capital for Treeland【思维+树型Dp】
- CF-219-D-Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
- phonegap开发hybrid app
- CodeForces 219C Color Stripe
- 最大连续子数列和问题
- Linux 图形界面与命令行模式切换
- 712 - S-Trees
- CodeForces 219D Choosing Capital for Treeland
- 解决.net 2.0以上版本GDI+绘图保存后在Photoshop中无法识别的问题
- POJ 1050(To the Max)
- 两点间最短路径及所有路径
- JavaScript学习笔记,创建对象,属性访问
- uva 10791 - Minimum Sum LCM
- 添加、读取、修改xml例子
- jQuery基础教程之强大的选择器(过滤选择器-基本过滤选择器)
- 备忘录模式(Memento)----行为笔记