Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
3 seconds
256 megabytes
standard input
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.
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 ≤ n; si ≠ 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.
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.
3
2 1
2 3
0
2
4
1 4
2 4
3 4
2
1 2 3
#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的更多相关文章
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- 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 ...
- Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)
- 构造 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 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- Codeforces Round #135 (Div. 2)
A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...
- Codeforces Round #657 (Div. 2) C. Choosing flowers(贪心)
题目链接:https://codeforces.com/contest/1379/problem/C 题意 有 $m$ 种花,每种花数量无限,第一次购买一种花收益为 $a_i$,之后每一次购买收益为 ...
- Codeforces Round #246 (Div. 2) A. Choosing Teams
给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...
- 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 ...
随机推荐
- Docker 介绍和安装(一)
# 下载阿里云的 Centos7 的docker.repo # step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-pers ...
- 【Linux】查看系统僵尸进程
ps -ef|grep -v grep|grep defunct 如果这个有显示内容的话,可以手动将进程kill掉即可 ---------------------------------------- ...
- SparkStreaming和Kafka基于Direct Approach如何管理offset实现exactly once
在之前的文章<解析SparkStreaming和Kafka集成的两种方式>中已详细介绍SparkStreaming和Kafka集成主要有Receiver based Approach和Di ...
- 利用sklearn进行字典&文本的特征提取
写在前面 这篇博客主要内容: 应用DictVectorizer实现对类别特征进行数值化.离散化 应用CountVectorizer实现对文本特征进行数值化 特征提取API sklearn.featur ...
- yml文件中${DB_HOST:localhost}的含义
引自:https://blog.csdn.net/chen462488588/article/details/109057342 今天学习eladmin项目中看到application-dev.yml ...
- Django--虛擬環境Virtualenv的安裝使用
Django--虛擬環境Virtualenv的安裝使用 本次隨筆只要記錄在windows下安裝virtualenvwrapper,以及簡單的使用命令. virtualenvwrapper的安裝 ...
- 对于Spring MVC 拦截器的一些了解
Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission lo ...
- How does Circus stack compare to a classical stack?
Frequently Asked Questions - Circus 0.15.0 documentation https://circus.readthedocs.io/en/latest/faq ...
- NULL-safe equal null 索引 空字符串
小结 1. mysql> INSERT INTO my_table (phone) VALUES (NULL); 有手机号但是不知道 mysql> INSERT INTO my_table ...
- TCMalloc源码学习(一)
打算一边学习tcmalloc的源码一边写总结文章.先从转述TCMalloc的一篇官方文档开始(TCMalloc : Thread-Caching Malloc). 为什么用TCMalloc TCMal ...