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. Skeleton Screen -- 骨架屏--应用

    案例:使用 现已经在支付的项目使用 用户体验一直是前端开发需要考虑的重要部分,在数据请求时常见到锁屏的loading动画,而现在越来越多的产品倾向于使用Skeleton Screen Loading( ...

  2. 多媒体开发(6):滤镜实现各种图片效果 | Video-Filters | avfilter | 变色

    之前讲过使用FFmpeg的drawtext滤镜(把图片或文字加到视频上),而实际上,FFmpeg的滤镜很强大,远不止加字幕或加图片的功能.滤镜是很有趣的,可以把图片变模糊.变色.缩放旋转,等等. 本文 ...

  3. 堡垒机jumpserver测试记录--安装

    一步一步安装(CentOS) 基本都是官网的东西,只是有些坑做了记录. http://docs.jumpserver.org/zh/docs/step_by_step.html 环境 系统: Cent ...

  4. Egret--添加一个精灵事件

    class Hello extends egret.DisplayObjectContainer{ //入口函数 private onAddStage(event:egret.Event){ //打开 ...

  5. linux下利用C或C++ 语言调用需要root权限的函数

    1.setuid法(1)登录root用户,将程序设置成root:root所有者(等价于:登录root用户编译程序).也可直接将普通用户加入root组中,那么编译程序不用来回切换用户.(2)登录root ...

  6. px,em,rem的区别与用法

    别人总结的.个人觉得特别的好: http://www.w3cplus.com/css/when-to-use-em-vs-rem.html

  7. Codeforces 785 - A/B/C/D/E - (Undone)

    链接:https://codeforces.com/contest/785 A - Anton and Polyhedrons #include<bits/stdc++.h> using ...

  8. java.util中,util是什么意思

    Util是utiliy的缩写,是一个多功能.基于工具的包. java.util是包含集合框架.遗留的 collection 类.事件模型.日期和时间设施.国际化和各种实用工具类(字符串标记生成器.随机 ...

  9. windows下创建.gitignore

    网上搜索 .gitignore 的创建,很多linux上的,而且还一样,... 尝试了几次,windows可以这样写 .svn/ .settings/ .buildpath .project

  10. Linux Shell 用法

    目录 Shell test 命令 数值测试 字符串测试 文件测试 函数返回值 Shell test 命令 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测 ...