嘟嘟嘟

树形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. JS实现二叉树的创建和遍历

    1.先说二叉树的遍历,遍历方式: 前序遍历:先遍历根结点,然后左子树,再右子树 中序遍历:先遍历左子树,然后根结点,再右子树 后续遍历:先遍历左子树,然后右子树,再根结点   上代码:主要还是利用递归 ...

  2. Ora-03113\Ora-03114与Oracle In 拼接字符串的问题

    刚深入接触Oracle不久(大学里以及刚参加工作时学到的Oracle知识只能算是皮毛),因为之前使用SqlServer有将近两年的时间,对SqlServer相对来说很熟悉,比较而言,Oracle真心很 ...

  3. python学习之老男孩python全栈第九期_day027知识点总结——反射、类的内置方法

    一. 反射 ''' # isinstance class A:pass class B(A):pass a = A() print(isinstance(a,A)) # 判断对象和类的关系 print ...

  4. Swift Development – List of Resources You Must Bookmark

    Ever since the introduction of iOS, there is iOS development fever across the globe. Many iOS develo ...

  5. Mac里用终端ssh远程连接Centos服务器

    在mac终端下输入 ssh -l root *.*.*.* 就可以远程连接Centos服务器了,端口没变还是:22 如果改变端口用下面方法输入: ssh -p 448(你改变的端口) -l root( ...

  6. drupal7区块内容对象

    区块内容对象

  7. apk 反编译 - 最新版图文教程

    apk 反编译 - 最新版图文教程 结合网上众多教程,整理一篇自己操作的,工具都是目前最新版 apk 反编译也就是将打包后的 apk 反编译为资源文件(图片).layout.样式.相关的实现代码等.( ...

  8. Python爬虫教程-06-爬虫实现百度翻译(requests)

    使用python爬虫实现百度翻译(requests) python爬虫 上一篇介绍了怎么使用浏览器的[开发者工具]获取请求的[地址.状态.参数]以及使用python爬虫实现百度翻译功能[urllib] ...

  9. JavaScript运算符优先级引起的bug

    [下面是昨天发给同事的邮件,为防止泄露商业机密,隐去了项目名和变量名] ==================================================== 昨天发现Nx代码中的一 ...

  10. 转载:Windows下三分钟搭建Shadowoscks服务器端

    Windows下三分钟搭建Shadowoscks服务器端 之前在V2EX上有人问为啥没人做个在Windows上一键运行Shadowsocks服务器端的程序,我只想说……这是因为没人关注我的libQtS ...