题意翻译
题目描述 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:
3
2 1
2 3
输出样例#1:
0
2
输入样例#2:
4
1 4
2 4
3 4
输出样例#2:
2
1 2 3

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

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

//Stay foolish,stay hungry,stay young,stay simple
#include<iostream>
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<<mn<<endl;
for(int i=1;i<=n;i++)
if(dis[i]==mn) cout<<i<<" ";
return 0;
}

[CF] 219D Choosing Capital for Treeland的更多相关文章

  1. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  2. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  3. 【题解】codeforces 219D Choosing Capital for Treeland 树型dp

    题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...

  4. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  5. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  6. Codeforces 219D Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...

  7. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

  8. Codeforces 219D Choosing Capital for Treeland 2次DP

    //选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...

  9. Codeforces 219D Choosing Capital for Treeland:Tree dp

    题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...

随机推荐

  1. POJ 2392【多重背包】

    题意: k个块,给出每个块的高度hi,数量ci,不能超过的高度: 求这些块可以组成的最大高度一个. 思路: 大致可看这个题是一个背包,背包的承重是高度. 对于每个物品,有他的价值是高度,还有限定的数量 ...

  2. codeforces 611C

    题意: 给你一个矩阵,矩阵里有" . "和" # "," . "表示空的," # "表示禁止的. 多米诺骨牌将占据正好有 ...

  3. android的logcat的message有字符长度的限制,超过将直接截断

    http://blog.csdn.net/qidizi/article/details/47291803 今天想在logcat中输出足够多的调试信息时,发现从logcat中返回的信息中明显少了后面一节 ...

  4. c#删除指定文件夹中今天之前的文件

    1.说明 使用Directory类对指定文件夹下的今天或者更早日期之前的文件进行删除.原文链接:http://www.cnblogs.com/lengzhan/p/6423943.html 2.代码 ...

  5. Oracle更新数据为MD5加密数据

    业务场景:在做安全等保问题,需要将原来保存的用户明文密码改成md5加密的密文密码,数据库是Oracle的 首先Oracle要管理员账号登录才可以调md5函数,具体函数是DBMS_OBFUSCATION ...

  6. 第十篇 .NET高级技术之委托

    委托是一种可以指向方法的数据类型,可以声明委托类型变量. 声明委托的方式:delegate返回值类型   委托类型名(参数) 比如delegate void MyDel(int n) 注意这里的除了前 ...

  7. PostgreSQL - 允许远程访问的设置方法

    原文转载至:PostgreSQL 允许远程访问设置方法 安装PostgreSQL数据库之后,默认是只接受本地访问连接.如果想在其他主机上访问PostgreSQL数据库服务器,就需要进行相应的配置. 配 ...

  8. C语言-------指针函数与函数指针的区别

    一. 在学习arm过程中发现这“指针函数”与“函数指针”容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数,即本质是一个函数.函数返回类型是某 ...

  9. Contextual Action bar(1) CAB in Android

    Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...

  10. SpringMVC之基于注解的Controller

    参考博客:https://www.cnblogs.com/qq78292959/p/3760560.html Controller注解: 传统风格的Controller需要实现Controller接口 ...