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 ...
随机推荐
- CentOS 上安装 GIT 服务
获取 YUM 中 GIT 信息: yum info git 查看当前 GIT 的版本: git --version 或 git version 卸载当前版本的 GIT: ...
- Android Recyclerview隐藏item的所在区域显示大空白问题的解决方案
最近搞了下Recyclerview,做了增加.删除item的功能.item上方有卡签 插个图片看下效果,点击底下的添加上去,同时,底下的item消失,这个用notifyItemInserted和not ...
- maven下nutz与servlet报错org.nutz.mvc.NutFilter cannot be cast to javax.servlet.Filter
使用maven搭建nutz时,加入servlet报错:org.nutz.mvc.NutFilter cannot be cast to javax.servlet.Filter 十二月 03, 201 ...
- X-Cart-5.3.1.4 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: x-cart-5.3.1.4 commercial ecommerce open-source x-cart 服务优惠价: 按服务商许可协议 云服务 ...
- ASP.NET写入和读取xml文件
xml是一种可扩展标记语言,在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如文章等.它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 ...
- 日志滚动与cron调度
日志滚动与cron调度 1.日志滚动 nginx默认日志不能进行滚动,始终写入到一个文件中,即access.log.编写日志滚动的shell脚本,并使用linux的cron定时调度周期性进行日志文件滚 ...
- PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法
哎呀呀,今天小仓鼠学到了魔术方法,简称魔法,哈哈哈哈,神经病啊~ 平时在面试的时候,也会遇到问魔术方法有哪些的问题哦!今天我们来了解一下下~ 1.__get() 形式: __get($objName) ...
- 在微信小程序里自动获得当前手机所在的经纬度并转换成地址
效果:我在手机上打开微信小程序,自动显示出我当前所在的地理位置: 具体步骤: 1. 使用微信jssdk提供的getLocation API拿到经纬度: 2. 调用高德地图的api使用经纬度去换取地址的 ...
- 将matlab处理结果保存为图像文件
imwrite(testIm, 'Data/Test/testIm.bmp', 'BMP');
- openstack cinder api对应的命令行接口
a) Create Volume cinder create 1 --display-name admin-volume1 cinder create --display-na ...