Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D
题目大意:
给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。
思路:先预处理一个点为根的代价,然后去dfs移动,总复杂度是O(n)
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int tot,go[],next[],first[],id[];
int son[],f[],v[],n;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y,int Id){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
id[tot]=Id;
}
void add(int x,int y){
insert(x,y,);insert(y,x,-);
}
void dfs(int x,int fa){
son[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
if (id[i]==-) v[pur]=;
dfs(pur,x);
son[x]+=son[pur]+v[pur];
}
}
void dp(int x,int fa){
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
if (id[i]==) f[pur]=f[x]+;
else f[pur]=f[x]-;
dp(pur,x);
}
}
int main(){
n=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
dfs(,);
f[]=son[];
dp(,);
int ans=n;
for (int i=;i<=n;i++) ans=std::min(ans,f[i]);
printf("%d\n",ans);
for (int i=;i<=n;i++) if (ans==f[i])
printf("%d ",i);
return ;
}
Codeforces 219D Choosing Capital for Treeland的更多相关文章
- (纪念第一道完全自己想的树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 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- Codeforces 219D Choosing Capital for Treeland:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- Codeforces 219D Choosing Capital for Treeland 2次DP
//选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)
题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
随机推荐
- 【转】java读写二进制文件的解决方法
原文网址:http://www.jb51.net/article/36940.htm 接口:Writerable 复制代码代码如下: package com.geoway.pad.common; im ...
- Codeforces 235E Number Challenge
http://codeforces.com/contest/235/problem/E 远距离orz......rng_58 证明可以见这里(可能要FQ才能看到) 还是copy一下证明吧: 记 $$f ...
- MySQL函数简介
//将时间戳长整形数值转换为yyyy-MM-dd HH:mm:ss格式SELECT FROM_UNIXTIME(CREATE_TIME, '%Y-%m-%d %H:%i:%S') FROM TBLS ...
- js中的call()与apply()
js中的call()函数和apply()函数: 1.主要作用:是用于指定作用域和传参 (1)用于指定作用域 window.color = "red"; var o = { colo ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
- Shell防DDOS攻击原理
#!/bin/sh #date:2015-12-13 #filename:fang-DOS.sh #version:v1.0 while true do #awk '{print $1}' ...
- Tree( 树) 组件[3]
本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 事件列表很多事件的回调函数都包含'node'参数, ...
- [MVC4-基礎] 從資料庫取值顯示在DropDownList中
剛開始學MVC4,以下是一些基礎的學習筆記! 完成效果像下面這樣,資料來源是既有的Database. 1.Controller public ActionResult Index() { SqlCon ...
- Apache Tomcat 7.0 Manager APP
解决Tomcat 7.0 进入项目管理页面时的密码问题 根据红框中的提示得知缺少manager管理角色,所以我们到Tomcat的conf文件夹下找到tomcat-users.xml文件,添加管理角色, ...
- Ueditor使用方法
1.到百度下载文件,有各种版本.下载.net版本 2.将所需文件导入工程中 分别是:themes文件夹.third-party文件夹.ueditor.all.min.js.ueditor.config ...