CodeForces 219D Choosing Capital for Treeland (树形DP)
题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少。并输出所有可能作为根的点。
思路:
先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用down[i]。还是那个点进行DFS,这次就要求答案了,尝试将每个点t作为根,那么以t作为根的总费用=down[t]+父亲这棵子树。down[t]已经在第一次DFS中求出,而父亲这棵子树就不是down[父亲]了,而是down[父亲]-down[t]+w(父亲,t)。注:w为边权。
#include <bits/stdc++.h>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=2e5+; struct node
{
int from,to,rev,next;
node(){};
node(int from,int to,int rev,int next):from(from),to(to),rev(rev),next(next){};
}edge[N*];
int head[N], n, edge_cnt;
void add_node(int from,int to,int rev)
{
edge[edge_cnt]=node(from,to,rev,head[from]);
head[from]=edge_cnt++;
} int down[N]; //down[i]表示是往下到点i
void DFS(int t,int far)
{
node e;
for(int i=head[t]; i!=-; i=e.next) //递归先算子树的
{
e=edge[i];
if(e.to^far) DFS(e.to, t);
}
int sum=;
for(int i=head[t]; i!=-; i=e.next) //再算自己的
{
e=edge[i];
if(e.to^far) sum+=down[e.to]+e.rev;
}
down[t]=sum; //只有1种情况:所有子树全部向下
} int ans[N], big;
void DFS2(int t,int far,int val)
{
node e;
ans[t]=down[t]+val; //以本节点为根
big=min(big, ans[t]);
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to^far)
{
int r=ans[t]-down[e.to]-e.rev+(e.rev^);
DFS2(e.to, t, r);
}
}
} void init()
{
memset(head, -, sizeof(head));
memset(down, 0x3f, sizeof(down));
edge_cnt=;
big=INF;
} int main()
{
//freopen("input.txt", "r", stdin);
init();
scanf("%d",&n);
for(int i=,a,b; i<n; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b,); //正向
add_node(b,a,);
}
DFS(,-);
DFS2(,-,);
printf("%d\n",big);
for(int i=; i<=n; i++) //输出解
if(big==ans[i])
printf("%d ",i);
return ;
}
AC代码
CodeForces 219D Choosing Capital for Treeland (树形DP)的更多相关文章
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- [codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...
随机推荐
- Android 常用adb shell 命令(转)
调试Android程序有时需要adb shell 命令,adb全称Android Debug Bridge ,就是起到调试桥的作用. 通过adb我们可以在Eclipse中通过DDMS来调试Androi ...
- 启动Android模拟器问题集锦
1.新建AVD时,报错“no CPU/ABI system image available for this target”, 解决:在SDK Manager中下载System Image 2.Sys ...
- TypeScript完全解读(26课时)_20.声明文件
首先学习识别已有的js库的类型 识别已有的js库的类型 UMD既可以作为全局库使用,也可以作为模块使用 先在着手来编写一个全局的库 新建文件 接收一个title,改变页面title的值 这里用到 &a ...
- 3-2if条件结构
不同条件做不同的操作.例如满100就减去20 条件结构 package com.imooc.operator; public class ConditionDemo1 { public static ...
- 也谈Flash mmorpg地图问题【转】
网上看一篇关于目前几个流行flash mmorpg地图实现的分析,这里也想说说自己的一些看法. 常见的三种方式:1.整图2.Tile元素拼装3.栅格化切片 整图 整图加载很好理解直接加载一张背景图.这 ...
- 微信小程序 设置宽度是100%,然后图片能成为正方形的方法。小程序按屏幕比例的正方形
1.在全局app.js中获取设备的宽度 globalData: { userInfo: null, sysWidth:wx.getSystemInfoSync().windowWidth, //图片宽 ...
- TP5之数据库备份
1.效果图 2.下载扩展类( \extands\org\Baksql.php) 3.在 \public\static 里新建一个data 文件夹用来存放 .sql 的文件 4.使用方法 con ...
- opencv学习资料
搜集一些基础书 数字图像处理 信号与系统 计算机视觉中的多视图几何 图像处理.分析与机器视觉 基于序列图像的视觉检测理论与方法 官网(各版本api) http://opencv.org/ opencv ...
- JS中的柯里化(currying) 转载自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]
JS中的柯里化(currying) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpr ...
- C 语言实例 - 删除字符串中的特殊字符
C 语言实例 - 删除字符串中的特殊字符 C 语言实例 C 语言实例 删除字符串中的除字母外的字符. 实例 #include<stdio.h> int main() { ]; int i, ...