Choosing Capital for Treeland CodeForces - 219D (树形DP)
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 - 1lines 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.
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.
Examples
3
2 1
2 3
0
2
4
1 4
2 4
3 4
2
1 2 3 题意:给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。
题解:树形dp,分别搜索一个点的子树的需要改变方向的个数(dfs1)和非子树需要改变方向的个数(dfs2)。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include <set>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 2e5+;
const ll mod = 1e9+; typedef pair<int,int>edge; vector<int>G[maxn];
set<edge>st;
int dp[maxn][];
/*
dp[i][0]该结点子树有多少条边要改,
dp[i][1]该结点的父节点有多少条边要改
*/
void dfs1(int u,int pre)
{
dp[u][] = dp[u][] = ;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(v == pre)
continue;
dfs1(v,u);
if(st.find(edge{u,v}) == st.end())//如果u不能到v则需要逆转边
dp[u][]++;
dp[u][] += dp[v][];
}
}
void dfs2(int u,int pre)
{
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == pre)
continue;
dp[v][] = dp[u][] - dp[v][] + dp[u][];//(dp[u][0]-dp[v][0])就是u的除v结点外的其他子树逆转边
if(st.find(edge{u,v}) != st.end())
dp[v][]++;
else
dp[v][]--;
dfs2(v,u);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
st.insert(edge{a,b});
G[a].push_back(b);
G[b].push_back(a);
}
dfs1(,);
dfs2(,);
int mn = INF;
for(int i=;i<=n;i++)
mn = min(mn,dp[i][] + dp[i][]);
printf("%d\n",mn);
for(int i=;i<=n;i++)
{
if(dp[i][] + dp[i][] == mn)
printf("%d ",i);
}
}
Choosing Capital for Treeland CodeForces - 219D (树形DP)的更多相关文章
- 树形dp(C - Choosing Capital for Treeland CodeForces - 219D )
题目链接:https://cn.vjudge.net/contest/277955#problem/C 题目大意:输入n,代表有n个城市,然后再输入n-1条有向边,然后让你找出一个改变边数的最小值,使 ...
- CodeForces 219D 树形DP
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- CF219D. Choosing Capital for Treeland [树形DP]
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 dfs
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
随机推荐
- java技术小白的入门
一.入门书籍 1,疯狂java讲义 2,java编程思想 3,Maven权威指南 4,Spring 3.0就是这么简单 5,Spring技术内幕 6,Spring实战 7,Maven实战 二.入门业务 ...
- (生产)better-scroll - 下拉刷新
Options 参数 startX: 0 开始的X轴位置 startY: 0 开始的Y轴位置 scrollY: true 滚动方向为 Y 轴 scrollX: 'true' 滚动方向为 X 轴 cli ...
- 转:ACCESS数据库转ORACLE数据库分享
来源: 作者:zz 网上有很多文章介绍access转oracle数据库的方法,本人都尝试了,不是很成功,列举一下,后来人不必盲目试了,基本不成功: 1.Access-->excel-->P ...
- ndk制作so库,ndk-build不是内部或外部命令。。。的错误
想了想大概就需要下面这几步: 1.下载ndk 2.配置ndk的环境变量 3.在android studio添加一些ndk的配置 4.编写c文件 5.生成so库 6.调用so库 上面提到的大部分问题你都 ...
- html+javascript+soap获取webservice免费天气预报信息
转自:http://blog.163.com/hubeimeiyu@126/blog/static/8004881020118303318687/ 首先,也是最重要的是互联网上免费的天气预报源:htt ...
- DEEP LEARNING 大满贯课程表
Reinforcement Learning post by ISH GIRWAN Courses/Tutorials Deep Reinforcement Learning, Spring 2017 ...
- COGS 2075. [ZLXOI2015][异次元圣战III]ZLX的陨落
★★☆ 输入文件:ThefallingofZLX.in 输出文件:ThefallingofZLX.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 正当革命如火如 ...
- make知识
makelist 语法 https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html CMakeLists.txt I am of t ...
- C++指针、引用、const
; int *p = &a; //定义指针p指向变量a *p = ; //*p代表a的值 ; p = &b; //p指向变量b *p = ; //此时*p代表b的值 ] = {,,}; ...
- IOS xcode 离线帮助文档安装和安装路径
将想要安装的xcode 帮助文档 版本,将 ‘帮助文档包’ 放入‘xcode 帮助文档安装路径’.再将Xcode软件重启. xcode 帮助文档安装路径: 在‘应用程序’->Xcode软件 右击 ...