(纪念第一道完全自己想的树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国的侵略战争.战火绵延五百里,在和平环境中 ...
随机推荐
- Sql Server——运用代码创建数据库及约束
在没有学习运用代码创建数据库.表和约束之前,我们只能用鼠标点击操作,这样看起来就不那么直观(高大上)了. 在写代码前要知道在哪里写和怎么运行: 点击新建查询,然后中间的白色空白地方就是写代码的地方了. ...
- Excel开发之旅(二)----数据的读写
1.要实现数据的读写,首先,我们需要添加引用: using Excel=Microsoft.Office.Interop.Excel; 直接在项目中添加即可. 2.给3个按钮添加响应事件,工程代码截图 ...
- 微服务~Eureka实现的服务注册与发现及服务之间的调用
微服务里一个重要的概念就是服务注册与发现技术,当你有一个新的服务运行后,我们的服务中心可以感知你,然后把加添加到服务列表里,然后当你死掉后,会从服务中心把你移除,而你作为一个服务,对其它服务公开的只是 ...
- 用css绘制各种图形
1.用css绘制三角形 http://www.cnblogs.com/blosaa/p/3823695.html
- Robberies hdu 2955 01背包
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu3507 Print Article(斜率DP优化)
Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...
- Android 实现UI设计
1. 计算屏幕高度,宽度代码(Activity中) DisplayMetrics outMetrics = new DisplayMetrics(); getWindowManager().getDe ...
- Sql Server 数据库中调用dll文件
1.首先新建一个空的解决方案,并添加一个类库,代码如下,编译并生产dll using System; using System.Collections.Generic; using System.Da ...
- WPF Popup全屏 弹出方法。解决只显示75%的问题。
WPF Popup全屏 弹出方法.解决只显示75%的问题. WPF 中 Popup 有一个特点.当Popup的高度超过屏幕的75%的时候,只显示75%的高度. 如下代码: <Window x ...
- scala PartialFunction
1.orElse和andThen的区别 源码如下,区别很明显,orElse是并列的关系,而andThen是调用者的结果作为k的输入. trait PartialFunction[-A, +B] ext ...