树形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. 首先就不妨设正 ...
随机推荐
- Day3 && Day4
本章内容对我来说真的是学的稀里糊涂的,除了前两题吭哧吭哧独立完成,第三题参考了别人的思路外,其余题目均是现学现卖,有点迷啊.所以写这篇博客的目的是先记录下聚聚们对本章内容相关重点的要求,并搜集一些相关 ...
- Python爬虫学习==>第五章:爬虫常用库的安装
学习目的: 爬虫有请求库(request.selenium).解析库.存储库(MongoDB.Redis).工具库,此节学习安装常用库的安装 正式步骤 Step1:urllib和re库 这两个库在安装 ...
- Django-ORM外键属性总结
ForeignKey ForeignKey(ForeignObject) # ForeignObject(RelatedField) to, # 要进行关联的表名 to_field=None, # 要 ...
- 【AMAD】python-goose -- HTML Content/Article 提取器
动机 简介 用法 个人评分 动机 新闻网页,结构大多是类似的. 所以,能不能用一种通用的爬取方法来提取其中的数据? 简介 Goose最初是一个Java项目,在2011年被转为了scala项目1. Py ...
- TCP/IP中32位IP地址与字符串转化
转载:http://www.cnitblog.com/wujian-IT/archive/2007/10/11/34739.aspx 在网络上面我们用的IP都是数字加点(192.168.0.1)构成的 ...
- Linux高级命令进阶(week1_day2)
Linux高级命令进阶(week1_day2)--技术流ken 输出重定向 场景:一般命令的输出都会显示在终端中,有些时候需要将一些命令的执行结果想要保存到文件中进行后续的分析/统计,则这时候 ...
- cisco路由的ip路由(一)
路由Corp配置Corp#sh run Building configuration... Current configuration : 1652 bytes ! version 12.4 no s ...
- [转帖]2018年的新闻: 国内首家!腾讯主导Apache Hadoop新版本发布
国内首家!腾讯主导Apache Hadoop新版本发布 https://blog.csdn.net/weixin_34194317/article/details/88811258 腾讯也挖了很多 ...
- Idea中通过Git将代码同步到GitHub
一.Idea中配置Git 点击IntelliJ IDEA->Preferences...->Version Control->Git->Path to Git executab ...
- IDEA 修改JavaWeb的访问路径
问题描述 对于我这个刚刚使用IDEA不久的新手来说,能够正常运行就不错了,不过到了后面,可能会觉得IDEA给你分配的默认访问路径很不顺手,比如访问的时候需要通过: http://loca ...