给你一棵有向树,需要选定一个点为capital,满足翻转边数最小

思路:先求出1为capital 的答案,然后向下更新孩子节点

dp[i]=dp[i-1]+judge(i);

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8
typedef __int64 ll; using namespace std;
#define INF 0x3f3f3f3f
#define N 200005 int dp[N];
int n; struct stud{
int to,ne,len;
}e[N*];
int head[N];
int num; inline void add(int u,int v,int len)
{
e[num].to=v;
e[num].len=len;
e[num].ne=head[u];
head[u]=num++;
} void dfs(int u,int pre)
{
dp[u]=;
for(int i=head[u];i!=-;i=e[i].ne)
{
int to=e[i].to;
if(to==pre) continue;
dfs(to,u);
dp[u]+=dp[to]+e[i].len;
}
} void dfsup(int u,int pre)
{
for(int i=head[u];i!=-;i=e[i].ne)
{
int to=e[i].to;
if(to==pre) continue;
dp[to]=dp[u];
if(e[i].len) dp[to]--;
else dp[to]++;
dfsup(to,u);
}
} int main()
{
int i,j;
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head));
num=;
int u,v;
i=n-;
while(i--)
{
scanf("%d%d",&u,&v);
add(u,v,);
add(v,u,);
}
dfs(,-);
dfsup(,-);
int ans=INF;
for(i=;i<=n;i++)
ans=min(ans,dp[i]);
printf("%d\n",ans);
vector<int>temp;
temp.clear();
for(i=;i<=n;i++)
if(dp[i]==ans) temp.push_back(i);
for(i=;i<temp.size();i++)
{
if(i) printf(" ");
printf("%d",temp[i]);
}
printf("\n");
}
return ;
}

树形DP Choosing Capital for Treeland的更多相关文章

  1. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  2. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  3. CF#135 D. Choosing Capital for Treeland 树形DP

    D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...

  4. CF219D. Choosing Capital for Treeland [树形DP]

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

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

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

  6. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

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

  7. Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

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

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

    传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional  ...

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

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

随机推荐

  1. office web apps安装部署,配置https,负载均衡(三)服务器连接域控制器

    前提条件:1>一台window server 2008R2 服务器 2>您已经在同一内网的另外一台服务器上安装好了域控制器文档请看我写的另外一篇文章: office web apps安装部 ...

  2. 关于Pytorch中accuracy和loss的计算

    这几天关于accuracy和loss的计算有一些疑惑,原来是自己还没有弄清楚. 给出实例 def train(train_loader, model, criteon, optimizer, epoc ...

  3. python笔记之python基础

    python基础语法 1.变量命名规则 1).变量必须以字母或者下划线_开头 2).变量可由字母.数字.下划线_组成 3).变量对大小写敏感 2.多行语句 当编写的代码较长,需要换行,可使用 \ 进行 ...

  4. 纯Js ——文字上下左右滚动

    ScrollBaseJs.js var $$ = function (id) { return typeof id == 'string' ? document.getElementById(id) ...

  5. 论文阅读 | Combating Adversarial Misspellings with Robust Word Recognition

    对抗防御可以从语义消歧这个角度来做,不同的模型,后备模型什么的,我觉得是有道理的,和解决未登录词的方式是类似的,毕竟文本方面的对抗常常是修改为UNK来发生错误的.怎么使用backgroud model ...

  6. Nexus2 yum插件RCE漏洞(CVE-2019-5475)

    1.漏洞简介: 安全研究员Christian August Holm Hansen在Nexus Repository Manager 2.X中披露了一个远程命令执行漏洞. 该漏洞默认具有部署权限帐户. ...

  7. [Python3] 034 函数式编程 匿名函数

    目录 函数式编程 Functional Programming 1. 简介 2. 函数 3. 匿名函数 3.1 lambda 表达式也称"匿名函数" 3.2 lambda 表达式的 ...

  8. PTA(Basic Level)1041.考试座位号

    每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考 ...

  9. 设计模式:模板方法(Template method)

    首先我们先来看两个例子:冲咖啡和泡茶.冲咖啡和泡茶的基本流程如下: 所以用代码来创建如下: 咖啡:Caffee.java public class Coffee { void prepareRecip ...

  10. python使用Flask作为MockServer的方法

    日常开发/测试过程中,需要对相关服务添加挡板--Mock 简单介绍一下使用python的Flask插件,对相关的服务进行Mock # coding:utf-8 import os from flask ...