Choosing Capital for Treeland

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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 ≤ nsi ≠ 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
Input
3
2 1
2 3
Output
0
2
Input
4
1 4
2 4
3 4
Output
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的更多相关文章

  1. 【题解】codeforces 219D Choosing Capital for Treeland 树型dp

    题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...

  2. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  3. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  4. 第一道防线__SpringMVC配置拦截器

    这几天在公司自己开发一个小系统,但是系统的安全性也得考虑,起初没注意,赶急就光关心业务逻辑和实现效果.最后老大一出手,就把最严重的问题指出来了,他说你这根本没安全性可言,于是我试着将公司使用的spri ...

  5. PTA 道长你想怎么死

    道长你想怎么死 (25 分) 故事:[ 他身着白衣,撑着伞朝我走来.说要送我回家.而我早已陷入他那对深邃的眼眸中,心内一阵悸动.他一把拉我入伞下.我得知他是山上的道士,也刚好下山采药.他把伞赠予我,一 ...

  6. HDU 2063 过山车 第一道最大二分匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=2063 题目大意: m个女生和n个男生一起做过山车,每一排必须一男一女,而每个女孩愿意和一些男生坐一起,, 你要找 ...

  7. 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 ...

  8. 【距离GDKOI:44天&GDOI:107天】【BZOJ1040】[ZJOI2008] 骑士 (环套树DP)

    其实已经准备退役了,但GDOI之前还是会继续学下去的!!当成兴趣在学,已经对竞赛失去信心了的样子,我还是回去跪跪文化课吧QAQ 第一道环套树DP...其实思想挺简单的,就把环拆开,分类处理.若拆成开的 ...

  9. BZOJ1040:骑士(基环树DP)

    Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...

随机推荐

  1. ThinkJS框架入门详细教程(一)开发环境

    一.前端标配环境 1.nodeJS正确安装,可以参考:http://www.cnblogs.com/chengxs/p/6221393.html 2.git正确安装,可以参考:http://www.c ...

  2. Musical Theme poj1743(后缀数组)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16757   Accepted: 5739 De ...

  3. M-自适应宽高样式

    1 绝对定位 position: absolute; top: 0px; bottom: 0px; left: 0px; width: 100%; overflow: hidden;

  4. Python GUI - Tkinter tkMessageBox

    Python GUI - Tkinter tkMessageBox: tkMessageBox模块用于显示在您的应用程序的消息框.此模块提供了一个功能,您可以用它来显示适当的消息     tkMess ...

  5. Jquery购物车jsorder改进版,支持后台处理程序直接转换成DataTable处理

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. WPF中的imagesource 和内存图片的处理

    [转载]ImageSource的使用心得 很多时候,我们会使用图片来装饰UI,比如作为控件背景等. 而这些图片可以分为两种形式,即存在于本地文件系统中的图片和存在于内存中的图片 对于这两种形式的图片, ...

  7. commons-pool与commons-pool2连接池

    commons-pool和commons-pool2是用来建立对象池的框架,提供了一些将对象池化必须要实现的接口和一些默认动作.对象池化之后可以通过pool的概念去管理其生命周期,例如对象的创建,使用 ...

  8. vue搭建项目前奏曲——vue-cli

    vue-cli是快速构建这个单页应用的脚手架,这个可是官方的.官方给的建议,如果你是初次尝试Vue,哪就老老实实用普通的书写引入js文件,这里牵扯太多的东西,例如webpack.Node.js.npm ...

  9. Hibernate逆向代码问题

    问题描述 使用hibernate tools 插件生成pojo代码非常方便快捷,但是我今天使用的时候发现,在eclips安装jboss插件后,找不到Hibernate Code Generation ...

  10. C# MessageBox.Show每隔3秒自动关闭

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...