//选择一个根使得变换最少边的方向使得能够到达所有点
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
const int MAXM = MAXN * ;
struct Edge {
int u,v,w;
int next;
}edge[MAXM];
int head[MAXN],tot; void init() {
tot = ;
memset(head,-,sizeof(head));
} void add_edge(int u,int v,int w) {
edge[tot].u = u;
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} int dp[MAXN],f[MAXN];
void dfs1(int u,int pre) {
dp[u] = ;
for (int i = head[u] ; i != - ; i = edge[i].next) {
int v = edge[i].v;
if (v == pre) continue;
dfs1(v,u);
dp[u] += dp[v] + edge[i].w;
}
} void dfs2(int u,int pre) {
for (int i = head[u] ; i != - ; i = edge[i].next) {
int v = edge[i].v,w = edge[i].w;
if (v == pre) continue;
f[v] = f[u] - w + ( - w);
dfs2(v,u);
}
} int main() {
int N;
while (cin >> N) {
init();
for (int i = ; i < N - ; i++) {
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v,);
add_edge(v,u,);
}
dfs1(,-);
f[] = dp[];
// cout << dp[1] << endl;
dfs2(,-);
int ans = N;
for (int i = ; i <= N ; i++) ans = min(ans,f[i]);
cout << ans << endl;
for (int i = ; i <= N ; i++) if (f[i] == ans) printf("%d ",i);
puts("");
}
return ;
}

Codeforces 219D Choosing Capital for Treeland 2次DP的更多相关文章

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

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

  2. Codeforces 219D Choosing Capital for Treeland:Tree dp

    题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...

  3. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  4. CodeForces 219D Choosing Capital for Treeland (树形DP)

    题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...

  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. Codeforces 219D. Choosing Capital for Treeland (树dp)

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

  7. Codeforces 219D Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...

  8. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

  9. CodeForces 219D Choosing Capital for Treeland (树形DP)经典

    <题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...

随机推荐

  1. Spark集群管理器介绍

    Spark可以运行在各种集群管理器上,并通过集群管理器访问集群中的其他机器.Spark主要有三种集群管理器,如果只是想让spark运行起来,可以采用spark自带的独立集群管理器,采用独立部署的模式: ...

  2. f3d源码解读

    Fomo3D 源码解析, 部署指南 https://www.meiwen.com.cn/subject/efntbftx.html 原文链接 Fomo3D 合约源码分析 准备工作 环境准备 (用于调试 ...

  3. JQuery Ajax执行过程AOP拦截

    JQuery Ajax过程AOP:用于在Ajax请求发送过程中执行必备操作,比如加载数据访问令牌. $.ajaxSetup({ type: "POST", error: funct ...

  4. 无法注册DLL/OCX:regsvr32 failed with exit code 0x5

    最近在服务器上装一个等值线控件,确报错: RegSvr32 failed with exit code 0x5   尝试解决方法1: 在cmd下 运行for %1 in (%windir%\syste ...

  5. w命令集合

    startx:在命令行模式下输入会进入图形界面 exit:注销Linux(以login shell登录会注销账号,以non-login shell登录会退出终端) data:显示日期和时间 data ...

  6. 完整Android开发基础入门博客专栏

    博客地址:http://www.runoob.com/w3cnote/android-tutorial-contents.html

  7. MapReduce 并行编程理论基础

    对于mapreduce这一并行计算模型,一直以来都不是很清楚其具体的执行细节,今天看了学院一位老师的实验指导书,对这一过程有了一个初步的理解,特别是map阶段和reduce阶段,所以做了一份笔记,现在 ...

  8. hadoop自定义数据类型

    统计某手机数据库的每个手机号的上行数据包数量和下行数据包数量 数据库类型如下: 数据库内容如下: 下面自定义类型SimLines,类似于平时编写的model import java.io.DataIn ...

  9. Byte数据类型—Java

    字节与字符 一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节,一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制最小值为0,最大值为255. UTF-8 ...

  10. springboot 学习笔记(二)--- properties 配置

    springboot可以提供了多种方式配置properties. 一.Java System.setProperty(k, v) System.setProperty("myname&quo ...