树形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 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
随机推荐
- JAVA里面的“指针”
JAVA里面的“指针” 众所周知,在java里面是没有指针的.那为何此处还要说java里面的“指针”呢?我们知道在C/C++中,指针是指向内存中的地址.那么在Java里 ...
- 【刷题】BZOJ 2346 [Baltic 2011]Lamp
Description 2255是一个傻X,他连自己家灯不亮了都不知道. 某天TZ大神路过他家,发现了这一情况, 于是TZ开始行侠仗义了. TZ发现是电路板的问题, 他打开了电路板,发现线路根本没有连 ...
- 【python】爬虫实践
参考链接 https://blog.csdn.net/u012662731/article/details/78537432 详解 python3 urllib https://www.jianshu ...
- 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径
51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...
- APT攻击基础科普
0x00 APT的历史起源背景 APT这个词汇最早起源于:2005年英国和美国的CERT组织发布了关于有针对性的社交工程电子邮件,放弃特洛伊木马以泄露敏感信息的第一个警告,尽管没有使用“APT”这个名 ...
- php高效遍历文件夹、高效读取文件
/** * PHP高效遍历文件夹(大量文件不会卡死) * @param string $path 目录路径 * @param integer $level 目录深度 */ function fn_sc ...
- bzoj : 4504: K个串 区间修改主席树
4504: K个串 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 268 Solved: 110[Submit][Status][Discuss] ...
- .net 控件开发常见的特性总结
http://blog.csdn.net/aofengdaxia/article/details/5924364 在.net开发中常常需要使用一些[]里面的特性描述,我发现对常用的几个知道大概的意思, ...
- Codeforces Round #302 (Div. 2) C 简单dp
C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...
- Rstdio快捷键总结
Rstdio常用快捷键总结 在R语言里面输入了一个不完整的指令以后 左下方的console pane一直有加号+,不管输入什么都是加号,只用推出重新打开Rstudio才行,后来google了下,按左上 ...