博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[CF] 219D Choosing Capital for Treeland
阅读量:6798 次
发布时间:2019-06-26

本文共 3124 字,大约阅读时间需要 10 分钟。

题意翻译题目描述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 n cities, some pairs of them are connected with unidirectional roads. Overall there are n-1 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 a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a 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 n ( 2<=n<=2·10^{
5} 2<=n<=2⋅10 5 ) — the number of cities in Treeland. Next 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} s i​ ,t i​ ( $ 1<=s_{i},t_{i}<=n; s_{i}≠t_{i} $ ) — the numbers of cities, connected by that road. The i i -th road is oriented from city s_{i} s i​ to city t_{i} t i​ . You can consider cities in Treeland indexed from 1 to n 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: 32 12 3输出样例#1: 02 输入样例#2: 41 42 43 4输出样例#2: 21 2 3

树形DP,先假设一个根1,然后求出每个子树里反边个数,再从上往下更新,考虑祖先的边。

min最好拿出来求,放在dfs里会导致求不到1号点。

//Stay foolish,stay hungry,stay young,stay simple#include
using namespace std;const int MAXN=400005;int n;struct Edge{ int next,to,w;}e[MAXN]; int ecnt,head[MAXN];inline void add(int x,int y,int w){ e[++ecnt].next = head[x]; e[ecnt].to = y; e[ecnt].w = w; head[x] = ecnt;}int dis[MAXN];int mn=1<<30;int dfs(int now,int pre){ int ret=0; for(int i=head[now];i;i=e[i].next){ int v=e[i].to; if(pre==v) continue; ret+=dfs(v,now)+e[i].w; } return dis[now]=ret;}void dfs2(int now,int pre){ for(int i=head[now];i;i=e[i].next){ int v=e[i].to; if(v==pre) continue; dis[v]=dis[now]+(e[i].w?-1:1);// mn=min(mn,dis[v]); dfs2(v,now); }}int main(){ cin.sync_with_stdio(false); cin.tie(0); cin>>n; for(int i=1;i<=n-1;i++){ int x,y; cin>>x>>y; add(x,y,0); add(y,x,1); } dfs(1,-1); dfs2(1,-1); for(int i=1;i<=n;i++) mn=min(mn,dis[i]); cout<
<

转载于:https://www.cnblogs.com/ghostcai/p/9247448.html

你可能感兴趣的文章
修炼你自己
查看>>
窥探一句话木马后门的背后
查看>>
Kafka设计解析(二):Kafka High Availability (上)-转
查看>>
bzoj2186【SDOI2008】沙拉公主的困惑
查看>>
Lambda 表达式的演示样例-来源(MSDN)
查看>>
什么场景应该用 MongoDB ?
查看>>
python学习:猜数字游戏
查看>>
Linux 进程、线程运行在指定CPU核上
查看>>
iOS11开发教程(二十三)iOS11应用视图实现按钮的响应(3)
查看>>
微软自然语言理解平台LUIS:从零开始,帮你开发智能音箱
查看>>
Centos创建用户
查看>>
视频列表
查看>>
python2 和 python3 区别
查看>>
cd4与cd8比值的意义
查看>>
【配置】log4j.properties 详解与配置步骤
查看>>
js页面载入特效如何实现
查看>>
C#委托和事件
查看>>
TPrinter控制票據打印機
查看>>
Pidgin 插件法解决Ubuntu11.10 QQ
查看>>
你好,WPF
查看>>