【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述
Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。
输入输出格式
输入格式
输入包含多个测试点。对于每个测试点,每个测试点的第一行为一个正整数n(2<=n<=2e5)。接下来n-1行,每行两个正整数ai,bi,表示城市a到城市b有一条单向通行的道路。输入以空格分隔,以EOF结尾。
输出格式
对于每个测试点,第一行输出k,第二行升序输出所有满足条件的首都的编号。
题目描述
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n−1 roads in the country. We know that if we don’t take the direction of the roads into consideration, we can get from any city to any other one.
The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.
Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
输入输出格式
输入格式:
The first input line contains integer $n$( $2<=n<=2·10^{5}$ ) — the number of cities in Treeland. Next n−1 n-1 n−1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers $s_{i}$,$t_{i}$ ( $1<=s_{i}$,$t_{i}<=n$; $s_{i}≠t_{i}$ ) — the numbers of cities, connected by that road. The $i$-th road is oriented from city $s_{i}$ to city $t_{i}$ . You can consider cities in Treeland indexed from 1 to $n$ .
输出格式:
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.
输入输出样例
输入样例#1: 复制
3
2 1
2 3
输出样例#1: 复制
0
2
输入样例#2: 复制
4
1 4
2 4
3 4
输出样例#2: 复制
2
1 2 3
思路
树型dp+两遍dfs
- 在第一次自底向上的dfs中,我们用$dp[u]$表示以u节点为首都时,u到其子树所有节点S需要逆转的边数
- 令正向边权值为0,反向边权值为1 得转移方程 $dp[u]=∑(dp[s]+w[i,s])$
- 第二次 $dp[i]$的定义变成了u到全树节点需要逆转的边数
分情况讨论
设 f为u的父亲
- 当f->u这条边是正向的时候,以u为首都则需要将这条边逆转, $dp[u]+=dp[f]+1$
- 当f->u这条边是反向的时候,以u为首都不需要将这条边逆转,但由于u是f的子节点,因此在dp[f]中将这条边逆转了,所以 $dp[u]+=dp[f]-1$
代码
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
struct edge {
int to,next;
bool w;
}e[400010];
int head[200010],tot,dp[200010];
inline void add(int from,int to,bool w) {
e[++tot]=(edge){to,head[from],w};
head[from]=tot;
}
void dfs1(int u,int fa) { //求u到子节点需要逆转的边数
for(int i=head[u];i;i=e[i].next) {
int v=e[i].to;
if(v==fa) continue;
dfs1(v,u);
dp[u]+=dp[v]+e[i].w;
}
}
void dfs2(int u,int fa) { //求u到全树需要逆转的边数
for (int i=head[u];i;i=e[i].next) {
int v=e[i].to;
if (v==fa) continue;
dp[v]=dp[u]+(e[i].w?-1:1);
dfs2(v,u);
}
}
int main() {
int n,a,b;
while (scanf("%d",&n)!=EOF) {
tot=0;
memset(head,0,sizeof(head));
for (int i=1;i<=n-1;i++) {
a=read(),b=read();
add(a,b,0);
add(b,a,1);
}
memset(dp,0,sizeof(dp));
dfs1(1,-1);dfs2(1,-1);
int Min=99999999;
for (int i=1;i<=n;i++) if (Min>dp[i]) Min=dp[i];
printf("%d\n",Min);
for (int i=1;i<=n;i++) if (Min==dp[i]) printf("%d ",i);
printf("\n");
}
return 0;
}
【题解】codeforces 219D Choosing Capital for Treeland 树型dp的更多相关文章
- 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:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)
题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- Codeforces 219D Choosing Capital for Treeland 2次DP
//选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...
- (纪念第一道完全自己想的树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
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
随机推荐
- 一、Github+Pycharm基础
GitHub为版本管理工具 常用的版本管理工具:本地化版本管理系统.集中式版本管理系统SVN.分布式版本管理系统 一.安装git(自行百度) 二.文件操作与分支管理基础 1.版本控制系统分类 集中化版 ...
- IOS Widget(5):小组件刷新机制
引言 前面的章节学完已经让我们可以顺利实现一个小组件了,但是小组件里面的数据如何刷新的呢,本节内容将讲解IOS的刷新机制. 大纲 系统如何管理小组件刷新 Timeline刷新机制 Timeline ...
- java+selenium使用JS、键盘滑动滚动条
本篇文章介绍如何使用JS和键盘对象对页面进行滑动滚动条-------------主要针对java做自动化测试的同学 一:使用键盘对象操作滚动条 //导包 import org.openqa.selen ...
- v-for详解
v-for的引入 当我们需要对一组数据进行渲染时,我们就可以使用v-for来完成 v-for遍历数组 格式:v-for="(item, index) in items".(也许是因 ...
- .NET Core 服务诊断工具
前言: 前一篇文中介绍了.NET Core-全局性能诊断工具 的使用方法,那么接下来自己实现一个简单.NET Core的诊断工具. 该工具主要包括:.NET Core 程序进程信息查看.性能计数器结果 ...
- c语言编程学习之字符串
字符串字面量与字符变量 1.字符串字面量 字符串字面量是一对双引号括起来的字符序列.当c语言编译器在程序中遇到长度为n的字符串字面量时,它会为字符串字面量分配长度为n+1的内存空间.这块内存空间用来存 ...
- YAML/YML文件一直提示格式错误解决方法
第一次接触yml文件,各种格式报错,但是看了几次也没看出来.其实有一个好方法,那就是直接通过yml在线格式检查 可以将yml具体内容复制到以下网址进行查询.具体报错位置会更加详细 https://ww ...
- SUSE12 网卡配置、SSH远程配置、解决CRT密钥交换失败,没有兼容的加密程序
安装好SUSE系统后发现网卡配置与Centos有些差异,多网卡的同学可以参考一下(我的是双网卡) SUSE系统默认第一块网卡自动获取IP,如果是多网卡,需要手动配置,由于我的第一个网卡获取正确无需更改 ...
- maven工具使用json-lib时,JSONArray.fromObject()不能执行的解决方案
前端代码我就不展示了 ,下面说明下我遇到问题的情况,如果不想看可以直接划到黄色字体部分直接找解决方法哦~~(相关jar包我会放在云中,想要的自己下载哦,链接在视频最下面!!) 我的pom文件,如下,导 ...
- 高德Serverless平台建设及实践
导读 高德启动Serverless建设已经有段时间了,目前高德Serverless业务的峰值早已超过十万QPS量级,平台从0到1,QPS从零到超过十万,成为阿里集团内Serverless应用落地规模最 ...