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 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
随机推荐
- Annotation(jdk5.0注解)复习(转自http://3w_cnblogs_com/pepcod/)
package annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retent ...
- 中文分词系列(一) 双数组Tire树(DART)详解
1 双数组Tire树简介 双数组Tire树是Tire树的升级版,Tire取自英文Retrieval中的一部分,即检索树,又称作字典树或者键树.下面简单介绍一下Tire树. 1.1 Tire树 Trie ...
- HDU1026 Ignatius and the Princess I
解题思路:打印路径是关键,细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> using ...
- 四:分布式事务一致性协议paxos通俗理解
转载地址:http://www.lxway.com/4618606.htm 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的"La" ...
- 使用svcutil.exe 生成服务的代码
@echo offcall "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"svcuti ...
- 解析AFNetWorking 网络框架(二)
转:http://blog.csdn.net/andy_jiangbin/article/details/17114989 接着前面写. 本帖先讲AFURLConnectionOperation,它是 ...
- 如何用HTML5+PhoneGap写个Path项目
最近Path这个应用很火爆,网上也出现了不少仿Path菜单的项目.即使在原生APP里边,Path的效果也是非常赞的.我突然想,Web APP是不是也能做出类似Path那样的效果呢?于是就有了OPath ...
- SQL注入常用语句
整形参数判断 1.直接加' 2.and 1=1 3. and 1=2 如果1.3运行异常 2正常就存在注入 字符型判断 1.直接加' 2.and '1'='1' 3. ...
- C#操作Excel,对Sheet插入次序的控制 (有待完善)
C#对Excel文件的操作,插入工作表(Worksheet)的方法是 Workbook.Worksheets.Add().通常情况下,我们在EXCEL的工作薄中,使用菜单操作:插入一个新的工作表,那么 ...
- [Everyday Mathematics]20150222
设 $$\bex a_0=1,\quad a_1=\frac{1}{2},\quad a_{n+1}=\frac{na_n^2}{1+(n+1)a_n}\ (n\geq 1). \eex$$ 试证: ...