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 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
随机推荐
- 51nod1204 Parity
如果sm[j]和sm[i]奇偶性相同,那么(i+1,j)个数为偶数如果奇偶性相同看成是朋友,不同的看成是敌人,那么就跟bzoj1370的做法差不多了. 如果奇偶性相同,就将x和y合并,x+n,y+n合 ...
- poj2750 线段树 +DP Potted Flower
问题描述:给定一个环形序列,进行在线操作,每次修改一个元素,输出环上的最大连续子列的和,但不能是完全序列. 算法:把环从一个地方,切断拉成一条直线,用线段树记录当前区间的非空最大子列和当前区间的非空最 ...
- BZOJ3028: 食物
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3028 题解:列出母函数乘起来化简之后再展开,用插板法即可. 代码: #include<c ...
- activiti 学习( 三 ) 之 流程启动者
在启动一个流程时,我们会有将当前用户启动的流程保存起来,作为流程发起人(启动人.申请人.提交人) 而在保存这个流程启动者信息,api 没有明确规范该怎么存.所以这里我总结下我学到的保存流程启动者信息的 ...
- Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? W ...
- 二:ZooKeeper术语概念
一:Zookeeper的设计目标 -->Zookeeper致力于提供一个高性能,高可用,且具有严格的顺序访问控制能力(主要是写操作的严格顺行性)的分布式协调服务. -->高性能使得Zo ...
- html:唤起手机qq开始对话 & 自动拨号
<a href="mqqwpa://im/chat?chat_type=wpa&uin=[qq号]&version=1">XXX</a> 另 ...
- Asp.net 身份验证方式?
[Forms 身份验证] 通过其可将没有通过身份验证的请求重定向到使用 HTTP 客户端重定向的 HTML 窗体的系统.用户提供凭据并提交该窗体.如果应用程序验证该请求,系统就会发出包含凭据或密钥的 ...
- lua的三目运算符
会lua的都知道三目运算符在lua中的写法是 a and b or c 但这里有个问题,就是当b是nil的时候会返回c的值 今天无意中看到一个大神的写法 (a and {b} or {c})[1] 不 ...
- selenium python (四)键盘事件
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #在实际测试过程中,有时候我们需要使用tab键将焦点转移到下一个需要操作 ...