CF 219 D Choosing Capital for Treeland(树形dp)
2015-08-21 17:44
288 查看
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 ≤ 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.
Sample test(s)
input
output
input
output
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 ≤ 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.
Sample test(s)
input
3 2 1 2 3
output
0 2
input
4 1 4 2 4 3 4
output
2 1 2 3
/* 这题就是给你一棵有向树,需要选定一个点为capital,满足翻转边数最小。 思路:先求出1为capital 的答案,然后向下更新孩子节点 */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8 typedef __int64 ll; using namespace std; #define INF 0x3f3f3f3f #define N 200005 int dp ; int n; struct stud{ int to,ne,len; }e[N*2]; int head ; int num; inline void add(int u,int v,int len) { e[num].to=v; e[num].len=len; e[num].ne=head[u]; head[u]=num++; } void dfs(int u,int pre) { dp[u]=0; for(int i=head[u];i!=-1;i=e[i].ne) { int to=e[i].to; if(to==pre) continue; dfs(to,u); dp[u]+=dp[to]+e[i].len; } } void dfsup(int u,int pre) { for(int i=head[u];i!=-1;i=e[i].ne) { int to=e[i].to; if(to==pre) continue; dp[to]=dp[u]; if(e[i].len) dp[to]--; else dp[to]++; dfsup(to,u); } } int main() { int i,j; while(~scanf("%d",&n)) { memset(head,-1,sizeof(head)); num=0; int u,v; i=n-1; while(i--) { scanf("%d%d",&u,&v); add(u,v,0); add(v,u,1); } dfs(1,-1); dfsup(1,-1); int ans=INF; for(i=1;i<=n;i++) ans=min(ans,dp[i]); printf("%d\n",ans); vector<int>temp; temp.clear(); for(i=1;i<=n;i++) if(dp[i]==ans) temp.push_back(i); for(i=0;i<temp.size();i++) { if(i) printf(" "); printf("%d",temp[i]); } printf("\n"); } return 0; }
相关文章推荐
- php:内存回收机制
- 新安装的wampserver怎么使用本机已有的mysql作为数据库
- c查找算法——线性索引查找
- 目前为止存在的这些问题
- python 打开文件,写入文件,关闭文件,读文件,清空文件内容
- Java基础——成员变量、局部变量和静态变量的区别
- 二进制-十进制-十六进制-含义解释
- JQuery插件开发之如何实时显示被隐藏的输入框密码
- SSH整合之架构的历史---用户注册
- [hive]Can't create/write to file '/tmp/#sql_2290_0.MYI' (Errcode: 13)
- 排序算法性能及选择总结
- Dmidecode命令详解
- 推荐一些好的网站,非常实用(业界推荐)
- jmeter上传文件搞了一天,才搞定,没高人帮忙效率就是低,赶紧记下来,以备后用
- 析构函数中的virtual是否必要?
- UI_iOS应用状态栏样式设置_及导航栏(NavigationBar)透明
- 只因你牛掰的能力20150821-5
- hdu1160 FatMouse's Speed (dp,记录路径)
- 复制一个JAVA WEB工程
- 【Swift】学习笔记(九)——枚举