Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs
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
题意:给你一棵树,有一个有向边,以一个点为根形成一棵树,求逆转的边数最小的所有根;
思路:以任意节点为根,dfs一遍求答案;正的边权为0,需要逆转的边权为1;
改成另外一个点的答案,即需要修改该点到根的这条链,ans-1的数目+0的数目;
两边dfs;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
#include<time.h>
using namespace std;
#define LL long long
#define pb push_back
#define mkp make_pair
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=,MOD=; int dp[N],ans=inf;
vector<pair<int,int> >edge[N];
void dfs(int u,int fa)
{
dp[u]=;
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i].first;
int w=edge[u][i].second;
if(v==fa)continue;
dfs(v,u);
dp[u]+=dp[v]+w;
}
}
void dfs(int u,int fa,int val)
{
dp[u]=dp[]-val;
ans=min(ans,dp[u]);
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i].first;
int w=(edge[u][i].second%?:-);
if(v==fa)continue;
dfs(v,u,val+w);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
edge[u].pb(mkp(v,));
edge[v].pb(mkp(u,));
}
dfs(,);
dfs(,,);
printf("%d\n",ans);
for(int i=;i<=n;i++)
if(ans==dp[i])printf("%d ",i); return ;
}
Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs的更多相关文章
- 树形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
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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 ...
随机推荐
- glog日志库移植Android平台
1.在linux平台下使用ndk交叉编译链编译glog生成libglog.a静态库. 2.将生成的库文件与头文件放到Android项目中,使用JNI方法调用. 3.编译遇到错误“stderr.stdo ...
- vue 实现modal
本文只是作为练习弹出框,弹框内部的东西需要进行自定义添加,主要对更新,删除,新建 ,提示四种弹框进行实现,例子中只是简单的组件应用 Modal.vue文件 <template> <d ...
- ogg 12.3 中 format release的变化
Non-CDB databases with compatibility set to 12.1, FORMAT RELEASE 12.2 or above is supported. Non-CDB ...
- @Entity 和 @Table
Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...
- sqlserver搜索中怎么把varchar类型转换成numeric类型
sqlserver搜索中怎么把varchar类型转换成numeric类型 可以用cast来转换 如:列名叫grade,表名为A select cast(grade as numeric(y,x)) f ...
- MyBatis框架入门之(二)
在本篇文章中,没有对细节进行处理的很好,有很多晓得细节的遗漏,本文只是一个简单的快速的入门 MyBatis的快速入门 导入MyBatis框架jar包 配置文件 SqlSessionFactoryBui ...
- Golang并发编程进程通信channel了解及简单使用
概念及作用 channel是一个数据类型,用于实现同步,用于两个协程之间交换数据.goroutine奉行通过通信来共享内存,而不是共享内存来通信.引用类型channel是CSP模式的具体实现,用于多个 ...
- P4219 [BJOI2014]大融合(LCT)
P4219 [BJOI2014]大融合 对于每个询问$(u,v)$所求的是 ($u$的虚边子树大小+1)*($v$的虚边子树大小+1) 于是我们再开个$si[i]$数组表示$i$的虚边子树大小,维护一 ...
- 【题解】Luogu P2783 有机化学之神偶尔会做作弊
原题链接:P2783 有机化学之神偶尔会做作弊 一看,是黑题,太毒瘤了,不写 什么单链??! 只会画有机化学中正六边形的我觉得这样不行QAQ(我才初二) 当然,题目也给你了详细的解释 实际呢,这道题先 ...
- python简说(十)json模块
常用模块: 一个python文件就是一个模块 1.标准模块,python自带的 2.第三方模块,需要安装 3.自己写的python文件 json,就是一个字符串 1.json转为字典 json_str ...