【CF】135 Div2 Choosing Capital for Treeland
树形结构,挺有意思的题目。不难。
/* 219D */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 2e5+;
int D[maxn];
int W[maxn], sw;
int W_[maxn];
vpii E[maxn];
vi vc;
int ans;
int n; void dfs(int u, int fa, int w_, int d) {
int v, w; D[u] = d;
W_[u] = w_;
rep(i, , SZ(E[u])) {
v = E[u][i].fir;
w = E[u][i].sec;
if (v == fa)
continue;
dfs(v, u, w_+w, d+);
W[u] += W[v] + w;
}
} void solve() {
// calculate w & c
dfs(, -, , );
ans = sw = W[];
vc.pb(); // calculate the minimum cost
int tmp; rep(i, , n+) {
tmp = sw - W_[i] + D[i] - W_[i];
if (tmp == ans) {
vc.pb(i);
} else if (tmp < ans) {
vc.clear();
ans = tmp;
vc.pb(i);
}
} printf("%d\n", ans);
rep(i, , SZ(vc))
printf("%d ", vc[i]);
putchar('\n');
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int u, v; scanf("%d", &n);
rep(i, , n) {
scanf("%d %d", &u, &v);
E[u].pb(mp(v, ));
E[v].pb(mp(u, ));
} solve(); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【CF】135 Div2 Choosing Capital for Treeland的更多相关文章
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- 【CF】323 Div2. D. Once Again...
挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- windows下Nginx配置与测试
1.获取 下载地址:http://nginx.org/en/download.html 参考网址:http://nginx.org/en/docs/windows.html 2.准备工作 ...
- 将Cell中的视图取出传递到根视图
当我们点击Cell中的某个图片时,图片会有一种从Cell中取出,放大,然后再回到原来的Cell中的效果.我的想法是:当Cell中的图片用button 来显示.当我们点击Cell中的这个button的时 ...
- redis基本数据类型【2】-Hash类型
一.概述 1.散列是一种典型的字典结构,filed和value的映射,但value只能存储字符串,不支持其他类型 2.一个散列类型最多包含 2^32 -1个字段 3.散列适合存储对象:使用对象和ID构 ...
- Fibonacci数列的java实现
关于Fibonacci应该都比较熟悉,0,1,1,2,3..... 基本公式为f(n) = f(n-1) + f(n-2); f(0) = 0; f(1) =1; 方法1:可以运用迭代的方法实现: p ...
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...
- HDU 2089 不要62(数位DP)
不要62 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了, ...
- unsigned int 转 RGB
unsigned int颜色存储格式:0xaabbggrr,其中a,b,g,r分别表示,透明度.蓝色.绿色.红色. 方法一:使用windows宏 unsigned int clr = 0x00FF00 ...
- [翻译][MVC 5 + EF 6] 2:基础的增删改查(CRUD)
原文:Implementing Basic CRUD Functionality with the Entity Framework in ASP.NET MVC Application 1.修改Vi ...
- Plugin 'FEDERATED' is disabled 或 1067错误 启动错误与“服务 mysql 意外停止”解决方法
MYSQL启动报1067错误,系统日志中是“服务 mysql 意外停止” Mysql日志中则是:“Plugin 'FEDERATED' is disabled” 网我在网上找到解决方案:1.在MY.I ...
- Linux的进程优先级
Linux的进程优先级 为什么要有进程优先级?这似乎不用过多的解释,毕竟自从多任务操作系统诞生以来,进程执行占用cpu的能力就是一个必须要可以人为控制的事情.因为有的进程相对重要,而有的进程则没那么重 ...