CF 219D

【题目链接】CF 219D

【题目类型】树形DP

&题意:

给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到u去,则要逆转该边方向)如果有多个这样的点,则升序输出所有。

&题解:

参考自:http://blog.csdn.net/angon823/article/details/52316220

需要求每个点到其他n-1个点是否要逆转路径,那么就可以把正的路cost=1,逆转的cost=0,这只要求这个点到其他点的cost,之后用所有的也就是n-1条边减去这个就好了。求一个点到其他点的cost,可以用树形DP来求,u点的cost=u子树的cost+其他的cost

&代码:

#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define cle(a,v) memset(a,(v),sizeof(a))
#define ll long long
const int maxn = 2e5 + 7; struct Edge {
int v, w, next;
} edges[maxn * 2];
int head[maxn], tot;
void addedge(int u, int v, int w) {
edges[tot] = Edge{v, w, head[u]};
head[u] = tot++;
}
int n, dpson[maxn], dpfa[maxn];
void dfs1(int u, int fa) {
for (int i = head[u]; ~i; i = edges[i].next) {
int v = edges[i].v;
int w = edges[i].w;
if (v != fa) {
dfs1(v, u);
dpson[u] += dpson[v] + w;
}
}
}
void dfs2(int u, int fa) {
for (int i = head[u]; i != -1; i = edges[i].next) {
int v = edges[i].v;
int w = edges[i].w;
if (v == fa) continue;
dpfa[v] = dpfa[u] + (w ? 0 : 1) + dpson[u] - dpson[v] - w;
dfs2(v, u);
}
}
int main() {
freopen("1.in", "r", stdin);
cle(head, -1); tot = 0;
cle(dpson, 0);
cle(dpfa, 0);
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v, 1);
addedge(v, u, 0);
}
dfs1(1, -1);
dfs2(1, -1);
vector<pair<int, int>> vp;
for (int i = 1; i <= n; i++) {
vp.push_back(make_pair(dpson[i] + dpfa[i], i));
}
sort(vp.begin(), vp.end());
int t1 = vp[n - 1].first;
int i;
for (i = n - 2; i >= 0; i--) {
if (vp[i].first != t1) {
break;
}
}
printf("%d\n", n - 1 - vp[n - 1].first);
for (int j = i + 1; j < n; j++) {
printf("%d%c", vp[j].second, j == n - 1 ? '\n' : ' ');
}
return 0;
}

CF 219D 树形DP的更多相关文章

  1. CodeForces 219D 树形DP

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

  2. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  3. CF EDU 1101D GCD Counting 树形DP + 质因子分解

    CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...

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

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

  5. CF 486D vailid set 树形DP

    As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are gi ...

  6. CF 337D Book of Evil 树形DP 好题

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  7. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

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

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

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

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

随机推荐

  1. CodeForces 553E Kyoya and Train 动态规划 多项式 FFT 分治

    原文链接http://www.cnblogs.com/zhouzhendong/p/8847145.html 题目传送门 - CodeForces 553E 题意 一个有$n$个节点$m$条边的有向图 ...

  2. vue v-cloak知识点

    1.使用 v-cloak 属性可以解决插值表达式闪烁问题;     2.v-text默认是没有闪烁的问题,同时会覆盖元素中原本的内容,但是v-cloak只会替换 自己的这个占位符,不会替换所有的字符 ...

  3. hibernate 报query result offset is not supported

    在配置hibernate.cfg.xml时需指定使用数据库的方言: 例: <property name="dialect">org.hibernate.dialect. ...

  4. oracle12c

    12c和11g环境是一样的,请参考前面 一.设置环境变量 vim /home/oracle/.bashrc ORACLE_BASE=/data/app/oracle ORACLE_HOME=$ORAC ...

  5. Spring-day02

    Annotation复习:1,Annotation:作为类型的元数据; 1,给类型加标记; 2,annotation可以添加各种类型的属性;2,Annotation的上的标记: 1),target:标 ...

  6. python学习-01

    1.编程语言分类: 编译型:(由编译器将代码编译成计算机识别的二进制文件)C \C++ \C#    运行速度较解释型语言快 解释型:(在运行时进行编译)python.php.sheel.ruby.j ...

  7. js滚动条滚动到底部触发事件

    $("#contain").scroll(function(){ var $this =$(this), viewH =$(this).height(),//可见高度 conten ...

  8. Ubuntu-1604-LTS在虚拟机设置分辨率

    在虚拟机中安装ubuntu系统时,有时系统的界面并不同虚拟机展示的匹配,需要我们进行调整.不用那么多废话,直接看图:

  9. python3 参数*args 、 **args 在函数定义和调用中的应用

    一.函数调用时 说明:*args 表示解包(解包 列表.元组.字符串类型) #定义函数cn_musql def cn_musql(host,port,user,pwd,db): print(host) ...

  10. Python自动化培训第一周学习总结

    Python自动化培训第一周学习结束,看视频复习,把作业完成了. 总体来说,开卷有益. 首先,工具真是好东西,能够极大提升效率,也是人区别于动物所在.想起前任大领导对工具的不屑,本质也是对效率的不屑, ...