time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first input line contains integer n (2 ≤ n ≤ 2·105)
— the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described
by a pair of integers si, ti (1 ≤ si, ti ≤ nsi ≠ ti)
— the numbers of cities, connected by that road. The i-th road is oriented from city si to
city ti.
You can consider cities in Treeland indexed from 1 to n.

Output

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.

Sample test(s)
input
3
2 1
2 3
output
0
2
input
4
1 4
2 4
3 4
output
2
1 2 3
题意:给你n个点所组成的树,每条边的方向是确定的,让你确定一个源点,使得为了使这个点能到达其他所有点改变的路径方向数最少。
思路:同样是用两次dfs搜一下就行了,第一次记录子树范围内的,第二次从父亲节点转移。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x7fffffff
#define maxn 200005
int first[maxn];
struct node{
int to,qidian,next;
}e[2*maxn];
int num[maxn],vis[maxn];
int minx; void dfs(int u)
{
int i,j,v;
int flag=0;
vis[u]=1;
for(i=first[u];i!=-1;i=e[i].next){
v=e[i].to;
if(vis[v])continue;
flag=1;
dfs(v);
if(e[i].qidian==u){
num[u]+=num[v];
}
else{
num[u]+=num[v]+1;
}
}
if(flag==0){
num[i]=0;return;
} } void dfs1(int u)
{
int i,j,v;
vis[u]=1;
for(i=first[u];i!=-1;i=e[i].next){
v=e[i].to;
if(vis[v])continue;
if(e[i].qidian==u){
num[v]=num[u]+1;
}
else{
num[v]=num[u]-1;
}
minx=min(minx,num[v]);
dfs1(v);
}
} int main()
{
int n,m,i,j,T,c,d,tot,flag1;
while(scanf("%d",&n)!=EOF)
{
tot=0;
memset(first,-1,sizeof(first));
for(i=1;i<=n-1;i++){
scanf("%d%d",&c,&d);
tot++;
e[tot].next=first[c];e[tot].to=d;e[tot].qidian=c;
first[c]=tot; tot++;
e[tot].next=first[d];e[tot].to=c;e[tot].qidian=c;
first[d]=tot; }
minx=inf;
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
dfs(1);
memset(vis,0,sizeof(vis));
minx=min(minx,num[1]);
dfs1(1);
printf("%d\n",minx);
flag1=1;
for(i=1;i<=n;i++){
if(num[i]==minx){
if(flag1==1){
flag1=0;
printf("%d",i);
}
else{
printf(" %d",i);
}
}
}
printf("\n");
}
return 0;
}

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland的更多相关文章

  1. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  2. 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 ...

  3. Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)

  4. 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!

    题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...

  5. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  6. Codeforces Round #135 (Div. 2)

    A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...

  7. Codeforces Round #657 (Div. 2) C. Choosing flowers(贪心)

    题目链接:https://codeforces.com/contest/1379/problem/C 题意 有 $m$ 种花,每种花数量无限,第一次购买一种花收益为 $a_i$,之后每一次购买收益为 ...

  8. Codeforces Round #246 (Div. 2) A. Choosing Teams

    给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...

  9. Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并

    E. Parking Lot time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. Logrotate工具使用

    Logrotate ​ logrotate是一个被设计来简化系统管理日志文件的工具,在系统运行时,如果产生大量的日志文件,可以使用该工具进行管理,如/var/log/*文件夹是存储系统和应用日志的目录 ...

  2. Memcached、Redis、Mongodb比较

    Memcached(内存Cache) Memcached 是一个高性能的分布式内存对象缓存系统.通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库 ...

  3. ps ww

    [root@ma ~]# ps ww -p 1 PID TTY STAT TIME COMMAND 1 ? Ss 0:01 /sbin/init[root@ma ~]# ps -p 1 PID TTY ...

  4. row和statement

    [root@ma bin]# /usr/local/mysql/bin/mysqlbinlog -vv /var/lib/bin/mysql-bin.000013 --base64-output=DE ...

  5. 【Linux】ssh互信脚本

    使用互信脚本的时候,需要敲回车,但是shell脚本无法满足,所以需要用到expect脚本 rpm -qa | grep expect 如果没有的话,直接用yum安装即可 yum install exp ...

  6. 关于阿里云服务器安装了Apache开放80端口访问不了网页

    先用netstat -tlunp查看80端口是否打开,再关闭服务器的防火墙,可以用 systemctl status firewalld 查看防火墙状态  systemctl stop firewal ...

  7. 阿里云OSS对象存储服务(二)

    一.使用SDK 在OSS的概览页右下角找到"Bucket管理",点击"OSS学习路径" 点击"Java SDK"进入SDK开发文档 二.创建 ...

  8. windows桌面快速添加控制面板网络等图标

    默认安装后的windows系统只有回收站. rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0

  9. RestTemplate post请求

    以前一开始用原生的http请求,那叫一个累,后来找到一个第三方的工具包,用起来是真的舒服,不过有一说一,第三方工具包依赖性真的强,除非和组长商量过,不然能少用,还是少用点.话说搞微服务的肯定少不了和H ...

  10. 竞态条件 race condition data race

    竞态条件 race condition Race condition - Wikipedia https://en.wikipedia.org/wiki/Race_condition A race c ...