传送门

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 ≤ 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,分别搜索一个点的子树的需要改变方向的个数(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)的更多相关文章

  1. 树形dp(C - Choosing Capital for Treeland CodeForces - 219D )

    题目链接:https://cn.vjudge.net/contest/277955#problem/C 题目大意:输入n,代表有n个城市,然后再输入n-1条有向边,然后让你找出一个改变边数的最小值,使 ...

  2. CodeForces 219D 树形DP

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  3. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  4. 【codeforce 219D】 Choosing Capital for Treeland (树形DP)

    Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...

  5. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  6. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  7. CF#135 D. Choosing Capital for Treeland 树形DP

    D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...

  8. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

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

随机推荐

  1. elasticsearch复杂查询-----2

    1.多条件查询 1)查询索引weibo下字段date大于或等于2015-09-05和name为Mary Jone的数据 2.简单查询 1)查询包含2014字符的数据 2)查询包含字符2014-09-1 ...

  2. IDEA插件JRebel安装配置与破解激活详细教程(转)

    JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...

  3. 从0到1构建适配不同端(微信小程序、H5、React-Native 等)的taro + dva应用

    从0到1构建适配不同端(微信小程序.H5.React-Native 等)的taro + dva应用 写在前面 Taro 是一套遵循 React 语法规范的 多端开发 解决方案.现如今市面上端的形态多种 ...

  4. canvas制作运动的小球

    <!DOCTYPE html> <head> <title>canvas</title> <style> .canvas{ border: ...

  5. 【Angular JS】正确调用JQuery与Angular JS脚本 - 修复Warning: Tired to load angular more than once

    自己正在做一个小网站,使用Angular JS + Express JS + Mongo DB,在开发过程中,遇到一些问题,所以整理出来.希望对大家都有帮助. 这是今天解决的一个问题,Angular ...

  6. Html + JS : 点击对应的按钮,进行选择是隐藏还是显示(用户回复功能)

    例如: 当我点击按钮1时,点击第一下进行显示This is comment 01,点击第二下隐藏This is comment 01 当我点击按钮2时,点击第一下进行显示This is comment ...

  7. IOS 数据加密总结(及MD5加密)

    数据安全总结 1.网络数据加密1> 加密对象:隐私数据,比如密码.银行信息2> 加密方案* 提交隐私数据,必须用POST请求* 使用加密算法对隐私数据进行加密,比如MD53> 加密增 ...

  8. sublime text 3 python 控制台输出中文乱码解决方案

    自建的python运行环境如下:python3 找到python3.sublime-build文件打开,在文件中加入"env": { "PYTHONIOENCODING& ...

  9. javaweb基础(38)_事务

    一.事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句  update from account set mone ...

  10. ASP.NET中无刷新分页

    上次介绍了我们代码写的刷新分页,这次就来说说无刷新分页. 这次我们是在上次的基础上改动了一些,我们都知道想要无刷新,就需要Ajax,在我们的ASP.NET中AJax是和一般处理程序配合着用的. 无刷新 ...