Tree with Small Distances(cf1029E)(树形动规)
You are given an undirected tree consisting of \(n\) vertices. An undirected tree is a connected undirected graph with \(n−1\) edges.
Your task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex 1 to any other vertex is at most 2 . Note that you are not allowed to add loops and multiple edges.
Input
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5 2≤n≤2⋅1e5\))-the number of vertices in the tree.
The following \(n - 1\) lines contain edges: edge \(i\) is given as a pair of vertices \(u_i, v_i\)(\(1 \le u_i, v_i \le n\)).
It is guaranteed that the given edges form a tree. It is guaranteed that there are no loops and multiple edges in the given edges.
Output
Print a single integer — the minimum number of edges you have to add in order to make the shortest distance from the vertex 1 to any other vertex at most 2 . Note that you are not allowed to add loops and multiple edges.
Sample Input1
7
1 2
2 3
2 4
4 5
4 6
5 7
Sample Output1
2
Sample Input2
71
1 2
1 3
2 4
2 5
3 6
1 7
Sample Output2
0
Sample Input3
7
1 2
2 3
3 4
3 5
3 6
3 7
Sample Output3
1
题意:
给你一棵树,让你从1往其他节点连边,使得1到任意节点的距离都小于等于2
题解:
我们设
\(dp[i][0]\)为不选\(i\)向根节点建边但以\(i\)为根的子树(包括\(i\))都被覆盖的最小费用
\(dp[i][1]\)为选\(i\)向根节点建边且以\(i\)为根的子树都被覆盖的最小费用
\(dp[i][2]\)为不选\(i\)向根节点建边但以\(i\)为根的子树(不包括\(i\))都被覆盖的最小费用
然后我们可以愉悦的列出DP方程
\(dp[i][1]=1+\sum_{j}^{j\in son_i}min(dp[j][0],dp[j][1],dp[j][2])\)
如果选这个点,它的儿子的状态就无关了,取最小值就可以了。
\(dp[i][2]=\sum_j^{j\in son_i}dp[j][0]\)
如果这个点不选且要使这个点不被覆盖,就只能取它的儿子的0状态更新。
这两条方程还是比较好推的。主要是0状态比较难转移。
我们可以分类,若他的儿子中有一个点\(j\)满足\(dp[j][1]<dp[j][0]\),就有
\(dp[i][0]=\sum_{j}^{j\in son_i}min(dp[j][0],dp[j][1])\)
注意这里不能用儿子的2状态转移,这会导致那个点不被覆盖
但如果没有儿子满足,我们可以在他的儿子中找一个点\(k\),使\(dp[k][1]-dp[k][0]\)最小,然后使
\(dp[i][0]=\sum_{j}^{j\in son_i}dp[j][0]\qquad +dp[k][1]-dp[k][0]\)
就行了 。
#include<bits/stdc++.h>
using namespace std;
int n;
int tot,bian[400010],nxt[400010],head[200010];
int v[200010];
inline int read(){
register char c;register int ret=0;
for(c=getchar();c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';ret=(ret<<1)+(ret<<3)+c-'0',c=getchar());
return ret;
}
inline void add(int x,int y){
++tot;bian[tot]=y;nxt[tot]=head[x];head[x]=tot;
}
int dp[200010][3];
void dfs1(int x,int f,int d){
for(int i=head[x];i;i=nxt[i]){
if(bian[i]==f)continue;
dfs1(bian[i],x,d+1);
}
for(int i=head[x];i;i=nxt[i]){
if(bian[i]==f)continue;
dp[x][1]+=min(min(dp[bian[i]][0],dp[bian[i]][2]),dp[bian[i]][1]);
if(dp[x][2]<1e9)dp[x][2]+=dp[bian[i]][0];
}
int mn=1e9,b=0;
for(int i=head[x];i;i=nxt[i]){
if(bian[i]==f)continue;
if(dp[bian[i]][1]<dp[bian[i]][0])dp[x][0]+=dp[bian[i]][1],b=1;
else dp[x][0]+=dp[bian[i]][0];
mn=min(mn,dp[bian[i]][1]-dp[bian[i]][0]);
}
if(!b)dp[x][0]+=mn;
if(d>1)dp[x][1]++;
}
int main()
{
// freopen("traffic.in","r",stdin);
// freopen("traffic.out","w",stdout);
n=read();
for(int i=1;i<n;++i){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs1(1,0,0);
cout<<min(dp[1][0],dp[1][1]);
}
Tree with Small Distances(cf1029E)(树形动规)的更多相关文章
- XJOI1571爱心蜗牛【树形动规】
爱心蜗牛 猫猫把嘴伸进池子里,正准备"吸"鱼吃,却听到门铃响了.猫猫擦了擦脸上的水,打开门一看,那人正是她的好朋友--川川.川川手里拿着一辆玩具汽车,对猫猫说:"这是我的 ...
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- P2015 二叉苹果树 (树形动规)
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...
- [LUOGU1122] 最大子树和 - 树形动规
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- 树形动规--没有上司的舞会--C++
题目来源:code[VS] 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个 ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- P2014 选课 (树形动规)
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
随机推荐
- NOIP水题测试(2017082301)
你们从题目也能看出来今天的题是很水的. 前几期答案还没出,效率有点低,谅解,谅解. 今天的答案应该会出的很快. 下面给题目: 时间限制:3小时 题目一:旅行家的预算 题目二:进制转换 题目三:乘积最大 ...
- JS高级-***Function- ***OOP
1. ***Function 作用域(scope): 什么是: 一个变量的使用范围 为什么: 避免函数内外的变量间互相影响 包括: 2种: 1. 全局作用域: window 保存着全局变量: 随处可用 ...
- Hibernate 的Configuration、sessionFactory和session和transaction对象解释
1.Configuration对象: Configuration conf=new Configuration(); conf.configure(); 1.1 到 src下面找到名称hibernat ...
- VMware设置inter共享连接出现空值
1.打开“网络和共享中心”选择“VMware Virtual Ethernet Adapter for VMnet8”网卡右键属性,选择VMware Bridge Protocol,同时设置ip自动获 ...
- 2018.12.18 bzoj2242: [SDOI2011]计算器(数论)
传送门 数论基础题. 对于第一种情况用快速幂,第二种用exgcdexgcdexgcd,第三种用bsgsbsgsbsgs 于是自己瞎yyyyyy了一个bsgsbsgsbsgs的板子(不知道是不是数据水了 ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- BZOJ [FJOI2007]轮状病毒 (找规律)
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 6009 Solved: 3282[Submit][Statu ...
- R语言的文件写入
R语言的文件写入 官方文档介绍如下: write.table(x, file = "", append = FALSE, quote = TRUE, sep = " &q ...
- hadoop sqoop的常用名命令
1 列出所有的ambari数据库中所有的表 Sqoop list-tables -connect jdbc:mysql://localhost:3306/ambari -username ambar ...
- HDMI之(AVI)Auxiliary Video Information InfoFrame
这是HDMI协议中关于AVI接口的信息 AVI InfoFrame在H14b中进行了定义.从HDMI 2.0 10.1 Use of the AVI InfoFrame in This Spe ...