Codeforces 1029 E. Tree with Small Distances(树上dp)
题目直通车:http://codeforces.com/problemset/problem/1029/E
思路大意:在树上做dp,依次更新ar数组,ar[i]表示以i为根节点的子树对答案的最小贡献值,依次更新即可,具体细节见代码
/*
13
1 2
1 3
1 4
4 5
4 6
4 7
7 8
7 9
7 10
10 11
10 12
10 13 output:2
*/ #include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout);
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
typedef long long ll;
typedef unsigned int un;
const int desll[][]={{,},{,-},{,},{-,}};
const ll mod=1e9;
const int maxn=2e5+;
const int maxm=1e6+;
const double eps=1e-;
int m,n;
int ar[maxn];
vector<int> ve[maxn];
int dis[maxn],ans,arTwo[maxn];
int sum,maxx;
//arTwo[i]表示i节点与1相连时,以i为根的子树中需要与1相连的个数
//ar[i]表示以i为根节点的子树中需要与1相连的个数,即对答案的贡献值的最少值,dp更新ar即可
int func(int u,int pre,int num){
int mid=;
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
if(num==)mid += func(v,u,num-);
else mid+=ar[v];
}
return min(mid, ar[u]);
}
void dfs2(int u,int pre){
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
dfs2(v,u);
}
arTwo[u] = func(u,pre,);
ar[u]=arTwo[u]+;//u节点与1相连的情况
int sumMid=,maxMid=-n;
for(int i=;i<ve[u].size();i++){//u节点的一个子节点与1相连的情况
int v=ve[u][i];
if(v==pre)continue;
sumMid += ar[v];
maxMid = max(maxMid, ar[v]-arTwo[v]-);
}
if(sumMid==)sumMid=,maxMid=;//特判子节点的情况
ar[u] = min(ar[u], sumMid-maxMid);
}
void funcDfs(int u,int pre,int num){
if(num==){
int mid=;
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
mid+=ar[v];
}
ans += min(ar[u], mid);
return ;
}
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
funcDfs(v,u,num-);
}
} int main()
{
scanf("%d",&n);
fill(ar,ar+n+,n);
fill(arTwo,arTwo+n+,n);
for(int i=;i<n;i++){
int a,b;scanf("%d%d",&a,&b);
ve[a].push_back(b);
ve[b].push_back(a);
}
ans=;
dfs2(,-);
funcDfs(,,);
printf("%d\n",ans);
return ;
}
Codeforces 1029 E. Tree with Small Distances(树上dp)的更多相关文章
- Codeforces 1111 E. Tree(虚树,DP)
题意 有一棵树,q个询问,每次询问,指定一个点做树根,再给定k个点,要求把这些点分成不超过m组的方案数,分配的限制是任意两个有祖先关系的点不能分在同一组.题目还保证了所有的询问的k加起来不超过1e5. ...
- CF E .Tree with Small Distances(树上的贪心)
题意: 这是一颗有n-1条边的无向树 , 在树上加最少的边使树的1节点到其他节点的距离最多为 2 : 分析:很容易考虑的贪心的做法,但是该如何的贪心呢 ? 我一开始是打算贪心节点的儿子最多那一个 , ...
- HDU6446 Tree and Permutation(树上DP)
传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- 树的最小支配集 E - Cell Phone Network POJ - 3659 E. Tree with Small Distances
E - Cell Phone Network POJ - 3659 题目大意: 给你一棵树,放置灯塔,每一个节点可以覆盖的范围是这个节点的所有子节点和他的父亲节点,问要使得所有的节点被覆盖的最少灯塔数 ...
- CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- 【题解】彩色树 51nod 1868 虚树 树上dp
Prelude 题目在这里:ο(=•ω<=)ρ⌒☆ Solution 蒟蒻__stdcall的第一道虚树题qaq. 首先很容易发现,这个排列是假的. 我们只需要求出每对点之间的颜色数量,然后求个 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
随机推荐
- HUAWEI TAG-AL00 找IMEI的过程
前几天,遇到一台华为机型,IMEI获取有问题,然后就找了一下. 以下是解决过程,权当记录一下,尽管为知笔记已经有备份了 :) 0x01: 起因 测试小哥发现,一台机型IMEI获取不全,有问题,拨号页面 ...
- javascript 数组的常用方法总结
前言 主要讨论一下数组的方法, 1.splice和slice的区别 2.pop和push 3.shift和unshift 4.join 5.forEach(es ...
- Python 3基础教程16-类
本文介绍类和简单使用,类是需要class这个关键字来声明的,一般如下面的语法: class className: def fun1(): pass def fun2(): pass 看下面demo.p ...
- Pytest框架介绍
Pytest框架介绍.安装 pytest是python测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,功能更强大 pytest特征 1:断言提示信 ...
- 使用pip命令报You are using pip version 9.0.3, however version 18.0 is available pip版本过期.解决方案
使用pip命令安装或卸载第三方库时报You are using pip version 9.0.3, however version 18.0 is available.错误,一般情况下是pip版本过 ...
- linux ubuntu开启sshd
which ssh #查看文件 sudo apt-get install ssh #安装ssh cd /etc/init.d #切换目录 ls -l | grep ssh #执行启动脚本 sudo s ...
- 如何在乌班图上配置java开发环境
不想说的那么细,每条命令都说一下,在现在这个浮躁的时代,很少有人能看的下去,我就直接上命令,最简单的快捷的方式. 1:安装软件 2:设置root密码 3:配置mysql远程登录 4:安装java运行环 ...
- python 多版本的兼容
1.针对linux版本 linux版本的话,首先调用whereis python 来获取到多版本的路径. root@Ulord-14:~# whereis pythonpython: /usr/bin ...
- 系统编程--高级IO
1.非阻塞I/O 非阻塞I/O使我们可以调用不会永远阻塞的I/O操作,例如open,read和write.如果这种操作不能完成,则立即出错返回,表示该操作如继续执行将继续阻塞下去.对于一个给定的描述符 ...
- 普通用户操作tomcat项目时报:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program
在使用普通用户更新tomcat项目适合出现这个信息,Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At ...