给你一棵有向树,需要选定一个点为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. 如何优雅的给TDatetimePicker控件赋值(Delphi)

    给DatetimePicker赋值时,可以通过界面设置赋值,也可以通过代码赋值. 通常,我们会给表示起始时间的dtp赋值为 00:00:00,给表示结束时间的dtp赋值为23:59:59. 代码如下: ...

  2. C#通过Oracle.ManagedDataAccess无法访问Oralce (转)

    原文转自:https://www.cnblogs.com/duanjt/p/6955173.html 问题描述:通过C#引用Oracle.ManagedDataAccess.dll访问Oracle,写 ...

  3. 解决 nw 报错 net::ERR_UNSAFE_PORT

    今天 nw 应用里面的前端请求突然不发送了,也没有异常的信息,后来换上开发版 nw 立刻就发现了报错:   net::ERR_UNSAFE_PORT   这个错误的意思很明显,就是请求的 http 端 ...

  4. Django 邮箱找回密码!!!!!!!!!!!!!!!!

    1.大概流程. @首先在完善登陆页面,增加忘记密码的链接. @为了账户安全,需要对操作者进行验证,向邮箱发随机数验证! @在重置验证码页面,验证验证码是否匹配(验证成功跳转至更改密码也页面). @ 重 ...

  5. Win10无线网络配置VMware的nat网络

    1.在windows上用运行cmd,用ipconfig /all查看可用网络的dns服务器 2.配置VMnet8,其dns与本地的dns服务器相同 3.打开VMware Workstation 的编辑 ...

  6. 【Linux】linux设备驱动归纳总结

    前言: (总结已经基本写完,这段时间我会从新排版和修正.错误总会有的,望能指正!) 前段时间学习了嵌入式驱动,趁着没开始找工作,这段时间我会每天抽出时间来复习. 我的总结是根据学习时的笔记(李杨老师授 ...

  7. luoguP1886 滑动窗口(单调队列模板题)

    题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #incl ...

  8. 2019牛客暑期多校训练营(第三场)- H Magic Line (计算几何)

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:给定n个点(n为偶数),求一条直线使得n个点平均分散在直线两端,即每端n/2个点. 思路:把n个点按 ...

  9. 比反射更快!使用ASM获取class信息(ClassReader)

    比反射更快!使用ASM获取class信息(ClassReader) 通常我们想要在java运行时获取class的信息时,通常使用反射的方式来获取其中的属性,方法,注解等信息.通常是这样的: Class ...

  10. 求问:numpy里面索引时,采用整型数组和整型列表的区别!