//选择一个根使得变换最少边的方向使得能够到达所有点
#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. 深度学习-CNN tensorflow 可视化

    tf.summary模块的简介 在TensorFlow中,最常用的可视化方法有三种途径,分别为TensorFlow与OpenCv的混合编程.利用Matpltlib进行可视化.利用TensorFlow自 ...

  2. iOS-Hello World

    尝试练习一些简单的app,能快速上手开发环境和开发流程.基础Start Developing iOS Apps (Swift)https://developer.apple.com/library/c ...

  3. Visual Studio 2012安装包

    点击下载

  4. css字体图标的制作和使用。

    css字体图标的制作和使用. 在项目开发的过程中,我们会经常用到一些图标.但是我们在使用这些图标时,往往会遇到失真的情况,而且图片数量很多的话,页面加载就越慢.所以,我们可以使用字体图标的方式来显示图 ...

  5. python完成简单购物功能

    # # -*- coding: utf8 -*- # # Author:wxq # # date:2017/11/13 # # python 3.6 # 创建一个商品列表: product_lis = ...

  6. centos7 centos6中 更改默认的系统启动级别

    centos6中更改默认的启动级别 方法: 1.vi /etc/inittab 2.找到id:x:initdefault:,我的系统是id:3:initdefault:,即默认以字符模式启动. 3.将 ...

  7. windows下 eclipse搭建spark java编译环境

    环境: win10 jdk1.8 之前有在虚拟机或者集群上安装spark安装包的,解压到你想要放spark的本地目录下,比如我的目录就是D:\Hadoop\spark-1.6.0-bin-hadoop ...

  8. WEBSTORM中html文件运行之后出现乱码的问题解决

    出现如下问题: 解决方案: 1.点击"文件编码" 2.选择GBK 3.点击Reload. 4.此时,源代码中的中文字体会变成乱码,把这些乱码重新输入成原先的中文.然后运行html文 ...

  9. POI 导入 一直报400问题

    排查过程:1.400一般都是参数或者请求不对,但是我这个情况是本地好用,只是服务器有问题,所以排除了传值的格式等问题. 2.服务器和本地网络隔离,所以没办法比较代码,分两次全量覆盖了html和js部分 ...

  10. 【Linux】如何设置Linux开机 ,默认进入图形界面或命令行界面?

    原创链接: https://blog.csdn.net/prophet10086/article/details/78501019 [7版本] 在root用户权限下: 查看当前启动模式 systemc ...