树形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. 首先就不妨设正 ...
随机推荐
- 2019 1月 第三次java基础有感
毕业半年了,一直在游戏公司做游戏服务器开发,java语言. 工作中,写着写着代码,接触java多了,有时候就会发现自己的java基础会不够用.以前实习的时候也体会到一次,然后过了一遍基础.现在正式工作 ...
- python分布式进程
分布式进程指的是将Process进程分布到多台机器上,充分利用多态机器的性能完成复杂的任务 分布式进程在python 中依然要用到multiprocessing 模块.multiprocessing模 ...
- robotframework-requests--中文注解版
最近工作原因在研究RobotFramework对REST测试的方案,找到几个相关类库.但使用requests感觉更方便,研究了一下requests类库的源码,并将注释换成中文为方便使用.关于Reque ...
- Windows-T
查看Windows系统版本号 同时按下Windows键和字母R键,然后输入winver就可以了 命令行运行计算器cmd-calc win10卸载XShell6报错1603 在运行里输入regedit打 ...
- UOJ#494K点最短路
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #d ...
- 【Python开发】Python中的class继承
继承是面向对象的重要特征之一,继承是两个类或者多个类之间的父子关系,子进程继承了父进程的所有公有实例变量和方法.继承实现了代码的重用.重用已经存在的数据和行为,减少代码的重新编写,python在类名后 ...
- java按某属性分组并计算相关属性的和。
工作中在处理集合的时候会经常遇到需要分组然后计算某属性的和,在java8中,通过stream来操作集合,还是非常方便的,像过滤(filter).分组(group).获取单个属性的值,总而言之,简单方便 ...
- 牛客小白月赛14 -A (找规律+除数取模)
题目链接:https://ac.nowcoder.com/acm/contest/879/A 题意:有n个城市,编号1~n,k天,第一天位于城市1,要求最后一天在城市1,且相邻两天不在同一个城市,求方 ...
- oracle数据区间
区是段下面的一个管理单位,一个区在物理上是一段连续的数据块. 一个数据文件有一个文件头,它用了若干个数据块,这个文件头里记录着区的分配与释放的信息.在这个文件中有些区是被使用的,有些区是空闲的. 什么 ...
- JAVA验证
1.一个JAVA类只能有一个主类. 2.main()方法返回值改为int,不能运行 3.变量作用域有限 实例: 4.数值类型在内存中直接存储其本身的值,对于不同的数值类型,内存中会分配相应的大小去存储 ...