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 ...
随机推荐
- python-数据分析与展示(Numpy、matplotlib、pandas)---3
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 0.pandas基于Numpy实现的,前者注重应用,后者注重结构 1.Series类型 ...
- scrapy_novel_python
# _*_ coding:UTF _8_ from bs4 import BeautifulSoup import requests,sys class downloader(object): def ...
- c#测试执行时间的方法
获取当前实例测量出来的总的运行时间 Stopwatch sp = new Stopwatch(); sp.Start(); //要测试的代码块 sp.Stop(); Console.WriteLine ...
- Qt QTextEdit根据行号移动光标
QTextEdit* p = new QTextEdit; QTextBlock block = p->document()->findBlockByNumber(nLineNum); p ...
- ELK学习笔记之Grok patterns正则匹配
https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns USERNA ...
- oracle 11g SKIP_UNUSABLE_INDEXES参数
SKIP_UNUSABLE_INDEXES的作用是在DML期间跳过对索引的维护,这样可以提交DML的效率,可以事后一次性重建,但是SKIP_UNUSABLE_INDEXES=y对unique inde ...
- phpstorm激活大全--持续更新(支持2018最新版)
方法一. 通过Licence Server 激活PHPStorm(快速) 方法原理是通过搭建服务器激活,不过网上有许多搭建好的. http://idea.goxz.gq http://v2mc.net ...
- Deep Learning Terminologies
Deep Learning Terminologies batch full batch 已知在梯度下降中,需要对所有样本进行处理过后然后走一步(梯度更新),那么如果我们的样本规模的特别大的话效率就会 ...
- web site optimization
@ 如果有很多图片(比如web服务器的页面上有多个小图片),通常是没有必要记录文件的访问时间的,这样就可以减少写磁盘的I/O,这个要如何配置 @ 首先,修改文件系统的配置文件/etc/fstab ,然 ...
- 【Python48--魔法方法:迭代器&生成器】
一.迭代器 1.iter() __iter__() 2.next() __next__() 二.用while语句实现for语句相同的功能 for each in range(5): print(eac ...