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 ...
随机推荐
- [精校版]The Swift Programming Language--语言指南--字符串和字符 (转)
今天装了10.10.马上就可以实际编写swift了.还是很兴奋啊. 哈哈.字符串和字符是大家最容易打交道的.今天就转一下讲解swift中字符串和字符的文章.希望对大家有帮助. 原文地址:http:// ...
- 在mac上使用github for mac 创建并上传项目
1.下载github for mac https://mac.github.com/ 2.登陆 偏好设置 3.用Xcode 创建一个项目,勾上“create local git respository ...
- springmvc+spring+mybatis+sqlserver----查询sqlserver----有返回参数
<resultMap type="java.util.HashMap" id="resultMap"> <result column=&quo ...
- jquery常用属性与方法
1..css( )给指定的样式设置样式值: 2..attr(attributeName,value) /.removeAttr(attributeName);给指定的属性设置值 / 清除所有匹配的元素 ...
- Windows之CMD查看系统信息
Windows 系统通过命令行(CMD)查询系统信息有两种方式: 1.图形化界面: 在“运行”中键入CMD,然后输入 dxdiag,回车后弹出图形化界面 ------ DirectX 诊断工具. 2. ...
- POS开发问题 - 缓存问题 - 02
问题描述 : 有一个A页面使用了缓存,当从别的页面返回到A页面时, A页面绑定的有些事件,例如监听输入框输入事件:"input",监听点击触摸屏事件:"touchstar ...
- 谷歌浏览器web worker出现cannot be accessed from origin 'null'错误
cannot be accessed from origin 'null'百度翻译是:无法从原点"null"访问 在别的浏览器都可以,而在唯独在谷歌浏览器不行,查找了一些资料原因大 ...
- Azure 1 月新公布
Azure 1 月新发布:Microsoft Power BI Embedded 公共预览和计算机视觉 API 标准版的更新以及 Azure IoT 网关 SDK 和中心设备管理新功能正式发布以及关于 ...
- Properties的使用以及配置文件值的获取
一.项目的部署如下,现在要获取SystemGlobals.properties中的值 二.代码如下: package com.util; import java.io.IOException; imp ...
- centos 卸载 docker
yum list installed | grep docker //查看安装过的包 docker-engine.x86_64 17.03.0.ce-1.el7.cen ...