树形DP Choosing Capital for Treeland
给你一棵有向树,需要选定一个点为capital,满足翻转边数最小
思路:先求出1为capital 的答案,然后向下更新孩子节点
dp[i]=dp[i-1]+judge(i);
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8
typedef __int64 ll; using namespace std;
#define INF 0x3f3f3f3f
#define N 200005 int dp[N];
int n; struct stud{
int to,ne,len;
}e[N*];
int head[N];
int num; inline void add(int u,int v,int len)
{
e[num].to=v;
e[num].len=len;
e[num].ne=head[u];
head[u]=num++;
} void dfs(int u,int pre)
{
dp[u]=;
for(int i=head[u];i!=-;i=e[i].ne)
{
int to=e[i].to;
if(to==pre) continue;
dfs(to,u);
dp[u]+=dp[to]+e[i].len;
}
} void dfsup(int u,int pre)
{
for(int i=head[u];i!=-;i=e[i].ne)
{
int to=e[i].to;
if(to==pre) continue;
dp[to]=dp[u];
if(e[i].len) dp[to]--;
else dp[to]++;
dfsup(to,u);
}
} int main()
{
int i,j;
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head));
num=;
int u,v;
i=n-;
while(i--)
{
scanf("%d%d",&u,&v);
add(u,v,);
add(v,u,);
}
dfs(,-);
dfsup(,-);
int ans=INF;
for(i=;i<=n;i++)
ans=min(ans,dp[i]);
printf("%d\n",ans);
vector<int>temp;
temp.clear();
for(i=;i<=n;i++)
if(dp[i]==ans) temp.push_back(i);
for(i=;i<temp.size();i++)
{
if(i) printf(" ");
printf("%d",temp[i]);
}
printf("\n");
}
return ;
}
树形DP Choosing Capital for Treeland的更多相关文章
- 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个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- 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 ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
随机推荐
- Ansible 直接请求远程主机执行命令
ansible -all -i host1.abc.com, -m ping #注意主机名称后面的逗号,就算一台主机也是必须的.多台主机可以用逗号隔开 ansible all -i host1.abc ...
- class文件访问标志
class文件中用两个字节共16位代表访问标志(access flags),用于表明该类或接口被访问时能提供的一些信息: 标志名称 标志值 含义 ACC_PUBLIC 0x00 01 是否为Publi ...
- 利用js代码自动删除稿件的普通弹幕
事情的起因是在b站投稿了一个高级弹幕测试的视频(av9940487),但是由于b站的弹幕池机制是新的弹幕顶掉旧的弹幕,所以导致一些人发的高级弹幕很快就被顶掉了. 所以就想着写个脚本来自动删除属性为普通 ...
- read: Connection reset by peer
造成此种问题的原因有多种,目前列下我所遇到过的. 1, sshfs usrname@172.23.65.122:/home/usrname ./122 在ubunutu 里面使用 sshfs 命令 映 ...
- 华为HCNA乱学Round 9:VRRP
- 【神经网络与深度学习】【C/C++】ZLIB学习
zlib(http://zlib.NET/)提供了简洁高效的In-Memory数据压缩和解压缩系列API函数,很多应用都会用到这个库,其中compress和uncompress函数是最基本也是最常用的 ...
- (转)Linux中显示空闲内存空间的free命令的基本用法
这篇文章主要介绍了Linux系统中free命令的基本用法,用free命令查看内存空余信息是Linux系统入门学习中的基础知识,需要的朋友可以参考下 free 命令显示系统使用和空闲的内存情况,包括 ...
- java追加文件
public class AppendToFile { /** * A方法追加文件:使用RandomAccessFile */ public static void appendMethodA(Str ...
- 【转帖】2018全球公有云IaaS榜单出炉:阿里、腾讯、中国电信、金山云列前十
2018全球公有云IaaS榜单出炉:阿里.腾讯.中国电信.金山云列前十 https://news.cnblogs.com/n/628391/ 中国电信貌似就是用的华为的技术 阿里 腾讯 华为 金山 百 ...
- mysql事件(event)
[小结]简单案例 SET GLOBAL event_scheduler=1delimiter $$ create definer = current_user event `test`.`event_ ...