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成为了首都,那么为了使首都能 ...
随机推荐
- 配置内核源码make menuconfig时出现 #include CURSES_LOC错误
配置内核时出现如下错误: liuxin@sunshine-virtual-machine:~/work/nfs_root/system/linux-2.6.22.6$ make menuconfig ...
- 二、cent OS安装配置tomcat
下载tomcat的tar包http://tomcat.apache.org/download-80.cgi 确保安装前已经安装JDKjava -version如果没有安装可以参考上一篇文章:http: ...
- java自学-流程控制语句
一件事情从开始到结束,需要先做什么,再做什么,最后再怎么做,这段过程就是一个流程.程序逻辑处理也是一个流程,java中有专门的流程控制语句,主要分为这几种:顺序,判断,选择,循环. 1.顺序流程 顺 ...
- HTTP的response code 1xx,2xx,3xx,4xx,5xx分别代表什么
HTTP 状态码点击链接可了解详情.您也可以访问 HTTP 状态码上的 W3C 页获取更多信息. 1xx(临时响应):表示临时响应并需要请求者继续执行操作的状态码. 2xx (成功):表示成功处理了请 ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- Java Future源码分析
JDK future框架,提供了一种异步编程模式,基于线程池的.将任务runnable/callable提交到线程池executor,返回一个Future对象.通过future.get()获取执行结果 ...
- PHP获取地址栏传的id值
function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- DOM基础操作实战代码
对于已经讲解给大家的DOM实战,我今天给大家几个实战代码,可以让大家加深对此的理解! 1.用DOM动态生成这样一个结构: <div class=”example”> <p class ...
- hihernate一对多关联映射
hihernate一对多关联映射 一对多关联映射利用了多对一关联映射原理 多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一 一对多关联映射:在多的一端加入一个外键指向一的一端 ...
- 解决API中无法使用session问题
处理API无法使用session的方法,贴图: 1调用如下图 2.需要在Global.asax文件中配置一些东西 protected void Application_PostAuthorizeReq ...