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. .NET探索平台条件编译

    前言 今天偶然机会,翻了一下大学期间的书籍<C程序设计>,好吧,当我翻着翻着,翻到了符号常量(#define指令)中,是啊,这是一个预处理器指令,记得在Magicodes.IE中针对平台选 ...

  2. 【Spring Boot】创建一个简单的Spring Boot的 Demo

    走进Spring Boot 文章目录 走进Spring Boot 环境搭建 新建Spring Boot项目 开始创建项目 配置JDK版本 和 Initializr Service URL 配置Proj ...

  3. C语言流程图画法(C语言学习笔记)

    常用符号及其含义 图片来自百度文库 https://wenku.baidu.com/view/beb410dea216147916112853.html 常用结构 N-S图

  4. 使用NIM Server网络半自动安装AIX系统

    一.NIM配置 1.安装NIMServer前准备 1.1.配置IP地址 # ifconfig –a #检查当前IP地址# # smitty mktcpip #设置IP地址# 选择第一块网卡(插网线的网 ...

  5. Nginx和Tomcat配置SSL实现https访问

    环境:CentOS 7 Nginx版本: nginx/1.18.0 1. 安装nginx 详细步骤可以参考如下官网:http://nginx.org/en/linux_packages.html#RH ...

  6. SAPCAR使用说明

    1.首先看一下SAPCAR的功能usage:create a new archive:SAPCAR -c[vir][f archive] [-P] [-C directory]   [-A filen ...

  7. 使用modify修改内表

    modify修改内表,有这样一种方式,MODIFY TABLE itab FROM wa [TRANSPORTING ..]. 然后这里的内表itab是有条件的,这个itab必须要有table key ...

  8. 注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途

    注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途 介绍: 如果你想将在SpringBoot项目中的配置类进行排序,那么用到spring-boot-au ...

  9. JavaScript小记

    JavaScript小记 1. 简介 1. 语言描述 JavaScript 是一门跨平台.面向对象的弱类型动态脚本编程语言 JavaScript 是一门基于原型.函数先行的语言 JavaScript ...

  10. css animation @keyframes 动画

    需求:语音播放动态效果 方案:使用如下图片,利用 css animation @keyframes  做动画 html <span class="horn" :class=& ...