树形dp(C - Choosing Capital for Treeland CodeForces - 219D )
题目链接:https://cn.vjudge.net/contest/277955#problem/C
题目大意:输入n,代表有n个城市,然后再输入n-1条有向边,然后让你找出一个改变边数的最小值,使得某个城市能够到达剩余的所有城市,然后问这样的城市有多少个,并且输出这些城市的编号。
具体思路:我们首先按照题目给的条件建好边,然后树就建好了,对于当前的某个节点,如果这个点要是能够到达剩余的所有城市,从这个节点往上的话,这个节点连接的父亲节点的这条边应该是是从子节点到父节点的。从这个节点往下的话,这个节点往下的话,他所连接的子节点应该是往下的,具体思路就来了。
用dp[i][0]代表当前的节点往下连接他的子树所需的最小的改变的边的数目。dp[i][1]代表的是当前的子节点往上连接他的父亲节点所需的改变最小的边数。
第二种情况,dp[rt][0]+=dp[soon][0];
第一种情况,dp[soon][1]+=dp[rt][1]+dp[rt][0]-edge[i].cost-dp[to][0]+(edge[i].cost==1?0:1).
AC代码:
#include<iostream>
#include<cmath>
#include<stack>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 4e5+;
struct node
{
int nex;
int to;
int cost;
} edge[maxn];
int num,head[maxn],dp[maxn][],father[maxn];
int sto[maxn];
void init()
{
num=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
}
void addedge(int fr,int to,int cost)
{
edge[num].to=to;
edge[num].nex=head[fr];
edge[num].cost=cost;
head[fr]=num++;
}
void dfs1(int fr,int rt)
{
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs1(to,fr);
dp[fr][]+=dp[to][]+edge[i].cost;
}
}
void dfs2(int fr,int rt)
{
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dp[to][]+=dp[fr][]+dp[fr][]-dp[to][]+(edge[i].cost==?:)-edge[i].cost;
dfs2(to,fr);
}
}
int main()
{
init();
int n;
scanf("%d",&n);
int t1,t2;
for(int i=; i<=n-; i++)
{
scanf("%d %d",&t1,&t2);
addedge(t1,t2,);
addedge(t2,t1,);
}
dfs1(,-);
dfs2(,-);
int minn=inf;
for(int i=; i<=n; i++)
{
minn=min(minn,dp[i][]+dp[i][]);
}
printf("%d\n",minn);
int num=;
for(int i=; i<=n; i++)
{
if(dp[i][]+dp[i][]==minn)
{
sto[++num]=i;
}
}
sort(sto+,sto+num+);
for(int i=; i<=num; i++)
{
if(i==)
printf("%d",sto[i]);
else
printf(" %d",sto[i]);
}
printf("\n");
return ;
}
树形dp(C - Choosing Capital for Treeland CodeForces - 219D )的更多相关文章
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- (纪念第一道完全自己想的树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 ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- 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 ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
随机推荐
- QObject 源代码阅读
我们进入 qt/src 文件夹.你可能对这里的目录名时曾相识,因为几乎这里的所有文件夹名都对应着 Qt 的模块的名字:gui,network,multimedia等等.我们从最核心的 QtCore 开 ...
- 【Java并发编程】之十一:线程间通信中notify通知的遗漏
notify通知的遗漏很容易理解,即threadA还没开始wait的时候,threadB已经notify了,这样,threadB通知是没有任何响应的,当threadB退出synchronized代码块 ...
- BZOJ3309 DZY Loves Math(莫比乌斯反演+线性筛)
一通正常的莫比乌斯反演后,我们只需要求出g(n)=Σf(d)*μ(n/d)的前缀和就好了. 考虑怎么求g(n).当然是打表啊.设n=∏piai,n/d=∏pibi .显然若存在bi>1则这个d没 ...
- SpringMVC框架并发时出现id变成另外一个用户id问题
今天测试写的代码,出现了在用一个账户登录操作的时候,操作记录的是另外一个id. 经过查找网上的解决方案确认了问题:在controller里面定义了一个userid属性,每次都通过userid传输值.然 ...
- c++11 语言级线程
c++11 语言级线程 线程的创建 用std::thread创建线程非常简单,只需要提供线程函数或函数对象即可,并且可以同时指定线程函数的参数. #define _CRT_SECURE_NO_WARN ...
- Merge Intervals - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...
- 毕业设计预习:VHDL入门知识学习(一) VHDL程序基本结构
VHDL入门知识学习(一) VHDL程序基本结构 简介 VHDL程序基本结构 简介 概念: HDL-Hardware Description Language-硬件描述语言-描述硬件电路的功能.信号连 ...
- java多线程 -- CountDownLatch 闭锁
CountDownLatch 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown ...
- Android Studio之BuildConfig类
https://blog.csdn.net/lvxiangan/article/details/71601451 Android Studio开发中,把一个module输出打包为jar文件,我们会发现 ...
- android 布局的两个属性 dither 和 tileMode
tileMode(平铺)tileMode(平铺) 的效果类似于 让背景小图不是拉伸而是多个重复(类似于将一张小图设置电脑桌面时的效果) dither(抖动) Dither(图像的抖动处理,当每个颜色值 ...