(纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland
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
/*
纪念自己第一道完全自己想出来的树DP!!!! 给n个城市,n-1单向道路,要从中选择一个首都,首都的要求是从首都能到达所有其他城市,如果这些单向路中的方向不合适,可以重修这条路;
问选择一个最合适的首都,是的修路的次数最少; 思路:建图时,将路标记正向为0,逆向为1,dp[u]表示以u为首都最少修的路,dfs出dp[1]然后就能得出一个状态转移方程dp[v]=dp[u]+(w?-1:1);
再用一次dfs就可以了
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#define N 200010
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int to,len;//下一个节点,长度
node (int x,int y)
{
to=x;
len=y;
}
};
int n;
vector<node> edge[N*];
int dp[N];
int ans=;
int dfs1(int u,int p)
{
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i].to;
int w=edge[u][i].len;
if(v==p) continue;
//cout<<"u="<<u<<" "<<"v="<<v<<" "<<"w="<<w<<endl;
ans+=w;
dfs1(v,u);
}
return ans;
}
void dfs2(int u,int p)
{
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i].to;
int w=edge[u][i].len;
if(v==p) continue;
//cout<<"u="<<u<<" "<<"v="<<v<<" "<<"w="<<w<<endl;
dp[v]=dp[u]+(w?-:);
dfs2(v,u);
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
edge[i].clear();
int a,b;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&a,&b);
edge[a].push_back(node(b,));
edge[b].push_back(node(a,));//逆向走的话就得花费一次
}
int min=INF;
ans=;
dp[]=dfs1(,-);
//cout<<"dp[1]="<<dp[1]<<endl;
dfs2(,-);
for(int i=;i<=n;i++)
{
if(dp[i]<min)
min=dp[i];
}
printf("%d\n",min);
int flag=;
for(int i=;i<=n;i++)
{
if(dp[i]==min)
{
printf(flag?" %d":"%d",i);
flag=;
}
}
printf("\n");
}
return ;
}
(纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland的更多相关文章
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- 第一道防线__SpringMVC配置拦截器
这几天在公司自己开发一个小系统,但是系统的安全性也得考虑,起初没注意,赶急就光关心业务逻辑和实现效果.最后老大一出手,就把最严重的问题指出来了,他说你这根本没安全性可言,于是我试着将公司使用的spri ...
- PTA 道长你想怎么死
道长你想怎么死 (25 分) 故事:[ 他身着白衣,撑着伞朝我走来.说要送我回家.而我早已陷入他那对深邃的眼眸中,心内一阵悸动.他一把拉我入伞下.我得知他是山上的道士,也刚好下山采药.他把伞赠予我,一 ...
- HDU 2063 过山车 第一道最大二分匹配
http://acm.hdu.edu.cn/showproblem.php?pid=2063 题目大意: m个女生和n个男生一起做过山车,每一排必须一男一女,而每个女孩愿意和一些男生坐一起,, 你要找 ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- 【距离GDKOI:44天&GDOI:107天】【BZOJ1040】[ZJOI2008] 骑士 (环套树DP)
其实已经准备退役了,但GDOI之前还是会继续学下去的!!当成兴趣在学,已经对竞赛失去信心了的样子,我还是回去跪跪文化课吧QAQ 第一道环套树DP...其实思想挺简单的,就把环拆开,分类处理.若拆成开的 ...
- BZOJ1040:骑士(基环树DP)
Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...
随机推荐
- go-fasthttp源码分析
1.架构 listener->server->workerpool 1.1.workerpool中有两种缓存: a.wp.ready,缓存未退出worker, b.worker退出后用sy ...
- springmvc 格式化返回日期格式
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-conve ...
- leetCode in Java (一)
前言 感觉写博客是一个很耗心力的东西T_T,简单的写了似乎没什么用,复杂的三言两语也只能讲个大概,呸呸...怎么能有这些消极思想呢QAQ!那想来想去,先开一个leetcode的坑,虽然已经工作了 ...
- 笨鸟先飞之ASP.NET MVC系列之过滤器(01过滤器简介)
过滤器 什么是过滤器? 过滤器(Filter) 主要的作用大致可以理解为把我们的附加逻辑注入到MVC框架的请求处理. 在ASP.NET MVC的请求处理中一种有19个管道事件分别是 BeginRequ ...
- hdu 2609 How many 最小表示法
How many Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- TCO之旅
TCO之旅 时间限制: 1 Sec 内存限制: 128 MB提交: 77 解决: 24[提交][状态][讨论版] 题目描述 我们的小强终于实现了他TCO的梦想了,爬进了TCO的全球总决赛,开始了他 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...
- Gradient Boost 算法流程分析
我们在很多Gradient Boost相关的论文及分析文章中都可以看到下面的公式: 但是,对这个公式的理解,我一直也是一知半解,最近,终于下决心对其进行了深入理解. 步骤1:可以看作优化目标的损失函数 ...
- 面向对象oop
类和对象 1.什么是类?什么是对象? 1)现实世界是由很多很多对象组成的 基于对象抽出了类 2)对象:真实存在的单个的个体 类:类型/类别,代表一类个体 3)类中可以包含: 3.1)所有对象所共有的属 ...
- Windows下如何创建低权限进程
1. 前言 在使用 Sysinternals 出品的 Process Explorer 过程中,对 “Run as Limited User” 功能的实现方式颇感兴趣,一番搜寻之下发现Mark ...