CF219D Choosing Capital for Treeland
树形dp。
首先一个很常规的想法就是如果u到v有一条边,那么建立cost(u, v) = 0, cost(v, u) = 1的两条边.
可以两遍dfs。
先任选一个点作为根节点,第一遍从下往上dfs,维护节点u到他的所有子节点的距离,很容易得出dis[u] = ∑dis[v] + cost(u, v) (v为u的儿子节点)。
第二遍从上往下dfs,维护节点u到所有节点的距离,考虑u和他的一个儿子节点v,两者到所有节点的区别只有cost(u, v)这条边是不一样的,如果cost(u, v) = 1,那么dis2[v] = dis2[u] - 1,否则dis2[v] = dis2[u] + 1.
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 2e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n;
vector<int> v[maxn];
vector<bool> c[maxn];
int dis[maxn];
bool vis[maxn]; void init()
{
for(int i = ; i <= n; ++i) v[i].clear(), c[i].clear();
Mem(dis, );
} void dfs1(int now)
{
vis[now] = ;
for(int i = ; i < (int)v[now].size(); ++i)
{
if(!vis[v[now][i]])
{
dis[now] += c[now][i];
dfs1(v[now][i]);
dis[now] += dis[v[now][i]];
}
}
}
void dfs2(int now)
{
vis[now] = ;
for(int i = ; i < (int)v[now].size(); ++i)
{
if(!vis[v[now][i]])
{
dis[v[now][i]] = dis[now] + (c[now][i] ? - : );
dfs2(v[now][i]);
}
}
} int main()
{
while(scanf("%d", &n) != EOF)
{
init();
for(int i = ; i < n; ++i)
{
int x = read(), y = read();
v[x].push_back(y); c[x].push_back();
v[y].push_back(x); c[y].push_back();
}
Mem(vis, ); dfs1();
Mem(vis, ); dfs2();
int Min = INF;
for(int i = ; i <= n; ++i) Min = min(Min, dis[i]);
write(Min); enter;
for(int i = ; i <= n; ++i) if(dis[i] == Min) write(i), space;
enter;
}
return ;
}
CF219D Choosing Capital for Treeland的更多相关文章
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 【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 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- [CF] 219D Choosing Capital for Treeland
题意翻译 题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能 ...
随机推荐
- HDFS学习
HDFS体系结构 HDFS采用了主从(Master/Slave)结构模型,一个HDFS集群包括一个名称节点(NameNode)和若干个数据节点(DataNode)(如图所示).名称节点作为中心服务器, ...
- Luogu 4240:毒瘤之神的考验
传送门 Sol 分开考虑 \(\varphi(ij)\) 中 \(ij\) 的质因子 那么 \[\varphi(ij)=\frac{\varphi(i)\varphi(j)gcd(i,j)}{\var ...
- border实现三角形的原理
前言:网上最普遍的实现三角形的方法,就是通过控制border来实现,那为什么可以呢? 原理 我们先来看看border的表现形式. #box{ width:100px; height:100px; ba ...
- js 判断 复选框全选、全不选、反选、必选一个
一个挺 使用的 js 代码片段, 判断 复选框全选.全不选.反选.必选一个 记录下, 搬来的 思路: 修改数据的 选中与否状态, 拿到所有的输入框,看是否有选中的状态 <html> & ...
- LintCode2016年8月22日算法比赛----将数组重新排序以构造最小值
将数组重新排序以构造最小值 题目描述 给定一个整数数组,请将其重新排序,以构造最小值. 样例 给定[3,32,321],通过将数组重新排序,可构造6个可能性的数字: 3+32+321=332321 3 ...
- RocketMQ读书笔记6——可靠性优先的使用场景
[顺序消息] 顺序消费是指消息的产生顺序和消费顺序相同. 比如订单的生成.付款.发货,这三个消息必须按顺序处理才可以. [顺序消息的分类] 全局顺序消息和部分顺序消息. 上面订单的例子,其实是部分顺序 ...
- EF Migrations
Enable-Migrations -EnableAutomaticMigrations dbcontent Add-Migration XXXXX Update-Database -Verbose ...
- Rxjava学习(一基础篇)
一.Rxjava跟EventBus的区别 RxJava 是一个响应式编程框架,通过一种扩展的观察者设计模式来实现异步操作. 跟AsyncTask和Handler类似,但是比AsyncTask和Hand ...
- Integer ==判断遇到的问题
今天开发过程中,遇到 这样的一个问题 public class Test { public static void main(String[] args) { Integer aa = 12345 ...
- IIS7 使用server farms 进行负载均衡
1.首先,到微软官网下载web平台安装程序: https://www.microsoft.com/web/downloads/ 2.安装好后,会在IIS里有这个图标: 3.双击这个图标:安装 4.安装 ...