嘟嘟嘟

树形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的更多相关文章

  1. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  2. 【codeforce 219D】 Choosing Capital for Treeland (树形DP)

    Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...

  3. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  4. 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 ...

  5. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  6. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  7. CF#135 D. Choosing Capital for Treeland 树形DP

    D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...

  8. Choosing Capital for Treeland CodeForces - 219D (树形DP)

    传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional  ...

  9. [CF] 219D Choosing Capital for Treeland

    题意翻译 题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能 ...

随机推荐

  1. word转pdf(使用office)

    1.安装office软件 2.在vs中写代码 注意需要引入 Microsoft.Office.Interop.Word插件 /// <summary> /// 将word转换成pdf文件 ...

  2. 最新sublime Text3 注册激活码

    sublime build 3103注册码 Enter License -- BEGIN LICENSE --Ryan ClarkSingle User LicenseEA7E-8124792158A ...

  3. BZOJ 3809Gty的二逼妹子序列 解题报告+data marker

    --BZOJ http://www.lydsy.com/JudgeOnline/problem.php?id=3809 考虑对l,r跑莫队,对一组维护美丽度出现次数的桶修改, 然后把桶序列用分块维护查 ...

  4. PHP学习笔记(二) ---- PHP数据类型

    PHP  __数据结构类型 一.php 中的八种数据类型 1.四种标量类型 Boolean (布尔类型): true  or  false,多用于条件判断. 实例: <?php $x = &qu ...

  5. 前端面试题1(html)

    HTML     * Doctype作用?严格模式与混杂模式如何区分?它们有何意义? 1.<!DOCTYPE> 声明位于文档中的最前面的位置,处于 <html> 标签之前.此标 ...

  6. KaTeX.js

    KaTeX.js 官网:https://khan.github.io/KaTeX/

  7. slice()方法 和splice 方法的区别

    定义 splice() 方法 用于插入.删除或替换数组的元素. slice() 方法 可提取字符串的某个部分,并以新的字符串返回被提取的部分. 更多的可查看: http://www.cnblogs.c ...

  8. MySQL数据库(6)----配置文件 my.cnf 的使用

    1. 使用源码安装好MySQL后,其配置文件一般位于 /usr/local/my.cnf,可以使用如下命令查看查看配置文件的搜索顺序: root@javis:~$ mysqld --help --ve ...

  9. Flume -- Transfer one type of source to another type

    Source within Flume is a kind of Server for outside client. Sink within Flume is a kind of client fo ...

  10. Java Basis

    java中.java源文件放在src文件夹下,.class文件放在bin文件夹下. java代码区域,以及控制台区域字体大小更改.Java->Java Editor Text Font      ...