Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D
树dp
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 2e5 + ;
struct Edge {
int next, to, cost;
}edge[N << ];
int head[N], tot, d[N], ans1[N], ans[N];
int res[N]; inline void add_edge(int u, int v, int c) {
edge[tot].next = head[u];
edge[tot].to = v;
edge[tot].cost = c;
head[u] = tot++;
} void dfs1(int u, int p) {
d[u] = ;
ans1[u] = ;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs1(v, u);
d[u] += d[v];
ans1[u] += edge[i].cost + ans1[v];
}
} void dfs2(int u, int p) {
ans[u] += ans1[u];
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
ans[v] = ans[u] - ans1[v] - edge[i].cost + !edge[i].cost;
dfs2(v, u);
}
} int main()
{
memset(head, -, sizeof(head));
int n, u, v;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%d %d", &u, &v);
add_edge(u, v, );
add_edge(v, u, );
}
dfs1(, -);
dfs2(, -);
int Min = N, cnt = ;
for(int i = ; i <= n; ++i) {
Min = min(Min, ans[i]);
}
for(int i = ; i <= n; ++i) {
if(ans[i] == Min) {
res[++cnt] = i;
}
}
printf("%d\n", Min);
sort(res + , res + cnt + );
for(int i = ; i <= cnt; ++i) {
printf("%d%c", res[i], i == cnt ? '\n': ' ');
}
return ;
}
Codeforces 219D. Choosing Capital for Treeland (树dp)的更多相关文章
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- (纪念第一道完全自己想的树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:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- Codeforces 219D Choosing Capital for Treeland 2次DP
//选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)
题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...
- Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
随机推荐
- 你其实真的不懂print("Hello,world")
http://www.jianshu.com/p/abb55919c453 debugPrint在发布的版本里也 会输出debugPrint只是更倾向于输出对象的调试信息.不管是开发环境还是测试环境都 ...
- ASP.NET MVC 4 WebAPI. Support Areas in HttpControllerSelector
This article was written for ASP.NET MVC 4 RC (Release Candidate). If you are still using Beta versi ...
- 用canvas实现图片滤镜效果
1.灰度效果 图片过滤效果之灰度效果 算法及原理: .299 * r + .587 * g + .114 * b; 2.油画效果 算法及原理: 用当前点四周一定范围内任意一点的颜色来替代当前点颜色,最 ...
- ecshop lib包含lib文件
在lbi文件中增加lbi方法 方法1. {include file='library/name.lbi '} 方法2. <?php echo $this->fetch('library/n ...
- 【英语】Bingo口语笔记(61) - mind系列
- mysql同步 小问题
由于历史遗留问题,我们的MySQL主从库的表结构不一致,主库的某个表tableA比从库表tableA少了一个字段. 当尝试在主库上更改表结构时,这行alter语句会随着binlog同步到从库,如果从库 ...
- iOS NSString的常用用法
//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astrin ...
- myeclipse9 struts2配置
引用struts2所用到的jar web.xml配置如下 <?xml version="1.0" encoding="UTF-8"?> <we ...
- cocos2dx+lua中cc.EventListenerMouse:create()的bug
今天在调试项目的时候用到了鼠标事件的监听 在创建事件监听器的时候出了问题 cc.EventListenerMouse:create() 这句返回值为nil 原来这是cocos2dx引擎的一个bug,t ...
- MSSQL 查询分组前N条记录
sql语句中,查询分组中前n条记录的sql语句如下 第一种方法 select * from consultingfeebill awhere n>(select count(*) from co ...