【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Description
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 Input
Input3
2 1
2 3Output0
2Input4
1 4
2 4
3 4Output2
1 2 3
【题意】
给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。
【分析】
啊啊啊看题意的时候不小心看到题解了。
啊啊啊我是不是很傻没有1秒钟看出来,数学老师说要一秒钟看出来的啊。
嗯。。先随便找一个点作为根,计算它的值,具体怎么计算呢,就是不指向它的边要加1。(一个dfs搞定)
然后从这个根往下走,往下走一步其实只有他们中间的边要方向,判断一下就好了。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 200010
#define INF 0xfffffff struct node
{
int x,y,c,next;
}t[*Maxn];int len=;
int first[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int ans=INF;
int op[Maxn],pl=;
int d[Maxn]; void dfs(int x,int f)
{
d[x]=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
dfs(y,x);
d[x]+=d[y]+t[i].c;
}
} void ffind(int x,int f)
{
if(d[x]<ans)
{
pl=;
op[++pl]=x;ans=d[x];
}
else if(d[x]==ans) op[++pl]=x;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
if(t[i].c==) d[y]=d[x]-;
else d[y]=d[x]+;
ffind(y,x);
}
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(x,y,);ins(y,x,);
}
dfs(,);
ffind(,);
sort(op+,op++pl);
printf("%d\n",ans);
for(int i=;i<=pl;i++) printf("%d ",op[i]);
printf("\n");
return ;
}
[CF 219D]
2016-10-17 15:04:38
【codeforce 219D】 Choosing Capital for Treeland (树形DP)的更多相关文章
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- [codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
随机推荐
- Centos如何设置静态IP地址,LINUX怎么修改IP地址
1.登陆连接centos系统,输入 ifconfig 可以查看到当前本机的IP地址信息 2.临时设置IP地址: 输入 ifconfig eth0 (默认是第一个网卡) 后面接IP地址, 网络掩码和 网 ...
- commons-fileupload源码学习心得
commons-fileupload依赖于commons-io包. commons-fileupload的使用方法: 1.创建一个文件项目工厂类DiskFileItemFactory. D ...
- Java 内存分析图
client -------------------------- public class Client{ public static void main(String[] args){ Perso ...
- Css3炫酷总结使用
先从CSS3的选择器说起: E F:所有的子孙元素: E>F: E中的子元素: E+F:E元素之后的最近的选择器: E~F:E中所以后面兄弟元素(CSS3 不包括自己本身,前面也不包括) att ...
- 并行计算vs分布式计算
一般认为,集中在同一个机柜内或同一个地点的紧密耦合多处理机系统或大规模并行处理系统是并行处理系统,而用局域网或广域网连接的计算机系统是分布式处理系统.松散耦合并行计算机中的并行操作系统有时也称为分布式 ...
- MVC中使用jquery的浏览器缓存问题
jquery在浏览器ajax调用的时候,对缓存提供了很好的支持,POST方式不能被缓存,使用POST的原因,明确了数据不能被缓存,或者避免JSON攻击(JSON返回数据的时候可以被入侵) jquery ...
- 手动修复OneDrive的DNS污染屏蔽的方法
随着云计算的发展和微软云战略的持续推进,使用网盘进行文档存储.协同编辑与共享已成为文档操作的新流程.而Office.Office 365和OneDrive等微软产品是Windows用户的首选.但由于国 ...
- Mysql下在某一列后即表的某一位置添加新列的sql语句
Mysql简介 MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司.MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤 ...
- 一个简单的定时器(NSTimer)的封装
在项目开发中我们有的时候需要用到计时器,比如登录超时,scrollview的滚动等,那么就让我们自己手动的去创建一个类库吧. 1 首先你需要一个向外提供创建的便捷方法. 1.1 这里考虑两种情况,一种 ...
- AutoLayout的一些注意事项
要了解autolayout 首先要知道程序视图启动顺序: -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; ...