bzoj 1912 tree_dp
这道题我们加一条路可以减少的代价为这条路两端点到lca的路径的长度,相当于一条链,那么如果加了两条链的话,这两条链重复的部分还是要走两遍,反而对答案没有了贡献(其实这个可以由任意两条链都可以看成两条不重叠的链来证明),那么这道题k=2的时候就转化为了求出树上两条链,使得两条链不重叠的长度最大,那么答案就是(n-1)<<1-SumLen+2.当k=1的时候我们直接求出来树的最长链然后减去就好了,这个在此不再赘述。
对于树上两链不重复部分最大我们是可以tree_dp的,设w[i][0..4]来表示当前以i为根的子树中选取了0/1/2条链的最大值,同时我们保留了一个3,4来记录以i为一端点的最长链,同时选取了0/1条最长链的最大值,这样直接转移就好了。
我写的是另外一种方法,先找出最长链,然后将最长链上的边长设为-1,然后再找一次最长链,这样求出来的就是答案。
反思:开始没意识到第二次最长链不能用两边bfs,所以果断的写了bfs,后来才发现的,又临时加了一个tree_dp,因为加的路必须选,所以我们要将每个点的最长和次长链设为-inf,叶子节点的为0,然后用非叶子节点更新答案,然后竟然1A,真是感动= =。
/**************************************************************
Problem: 1912
User: BLADEVIL
Language: C++
Result: Accepted
Time:1268 ms
Memory:5884 kb
****************************************************************/
//By BLADEVIL
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
#define maxm 200020
#define inf (~0U>>1)
using namespace std;
int n,k,l;
int pre[maxm],other[maxm],last[maxn],len[maxm];
int que[maxn],dis[maxn],father[maxn],flag[maxn],max_1[maxn],max_2[maxn];
void connect(int x,int y) {
pre[++l]=last[x];
last[x]=l;
other[l]=y;
len[l]=;
}
void bfs(int x) {
memset(que,,sizeof que);
memset(dis,,sizeof dis);
memset(father,,sizeof father);
memset(flag,,sizeof flag);
int h=,t=;
que[]=x; dis[x]=; flag[x]=;
while (h<t) {
int cur=que[++h];
for (int p=last[cur];p;p=pre[p]) {
if (flag[other[p]]) continue;
father[other[p]]=p;
dis[other[p]]=dis[cur]+len[p];
flag[other[p]]=;
que[++t]=other[p];
}
}
}
int tree_dp() {
int ans=-inf;
memset(que,,sizeof que);
memset(flag,,sizeof flag);
memset(dis,,sizeof dis);
memset(max_1,-,sizeof max_1);
memset(max_2,-,sizeof max_2);
int h=,t=;
que[]=; flag[]=; dis[]=;
while (h<t) {
int cur=que[++h];
for (int p=last[cur];p;p=pre[p]) {
if (flag[other[p]]) continue;
que[++t]=other[p]; flag[other[p]]=; dis[other[p]]=dis[cur]+;
}
}
//for (int i=1;i<=n;i++) printf("%d ",que[i]); printf("\n");
for (int i=n;i;i--) {
int cur=que[i];
for (int p=last[cur];p;p=pre[p]) {
if (dis[other[p]]<dis[cur]) continue;
if (max_1[other[p]]+len[p]>max_1[cur])
max_2[cur]=max_1[cur],max_1[cur]=max_1[other[p]]+len[p]; else
if (max_1[other[p]]+len[p]>max_2[cur])
max_2[cur]=max_1[other[p]]+len[p];
}
if (max_1[cur]<-) max_1[cur]=max_2[cur]=; else ans=max(ans,max(max_1[cur]+max_2[cur],max_1[cur]));
}
//for (int i=1;i<=n;i++) printf("|%d %d\n",max_1[i],max_2[i]);
return ans;
}
int getmax() {
int s=;
for (int i=;i<=n;i++) if (dis[i]>dis[s]) s=i;
return s;
}
int main() {
scanf("%d%d",&n,&k); l=;
for (int i=;i<n;i++) {
int x,y; scanf("%d%d",&x,&y);
connect(x,y); connect(y,x);
}
bfs(); bfs(getmax());
if (k==) {
printf("%d\n",*n-dis[getmax()]);
return ;
}
int cur=getmax(),ans=dis[cur]-;
while (father[cur]) len[father[cur]]=len[father[cur]^]=-,cur=other[father[cur]^];
ans+=tree_dp()-;
printf("%d\n",*n--ans);
return ;
}
bzoj 1912 tree_dp的更多相关文章
- BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...
- [BZOJ 1912] patrol 巡逻
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1912 Algorithm: K=0:res=(n-1)*2 每条边恰好走2遍 K=1 ...
- BZOJ 1912:[Apio2010]patrol 巡逻(树直径)
1912: [Apio2010]patrol 巡逻 Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ ...
- BZOJ 1912 巡逻
重赋边权. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- bzoj 1912 巡逻(树直径)
Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...
- bzoj 1912 : [Apio2010]patrol 巡逻 树的直径
题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...
- BZOJ 1912 巡逻(算竞进阶习题)
树的直径 这题如果k=1很简单,就是在树的最长链上加个环,这样就最大化的减少重复的路程 但是k=2的时候需要考虑两个环的重叠部分,如果没有重叠部分,则和k=1的情况是一样的,但是假如有重叠部分,我们可 ...
- 题解 BZOJ 1912 && luogu P3629 [APIO2010]巡逻 (树的直径)
本来抄了篇题解,后来觉得题解都太不友好(我太菜了),一气之下自己打...一打打到第二天QAQ 首先什么边也不加时,总路程就是2*(n-1) 考虑k=1的时候,答案显然是2*(n-1)-直径+1=2*n ...
- bzoj 1912: [Apio2010]patrol 巡逻【不是dp是枚举+堆】
我是智障系列.用了及其麻烦的方法= =其实树形sp就能解决 设直径长度+1为len(环长) 首先k=1,直接连直径两端就好,答案是2*n-len 然后对于k=2,正常人的做法是树形dp:先求直径,然后 ...
随机推荐
- 3ds Max学习日记(三)
今天把第三章搞完了,学的是样条线(splines)建模的一些操作.不过实习又有新任务了,得去研究一下如何将单张图片转化为三维模型(我擦,这神马操作),所以可能没有那么多时间愉快地与3ds max玩 ...
- 解决爬虫浏览器中General显示 Status Code:304 NOT MODIFIED,而在requests请求时出现403被拦截的情况。
在此,非常感谢 “完美风暴4” 的无私共享经验的精神 在Python爬虫爬取网站时,莫名遇到 浏览器中General显示 Status Code: 304 NOT MODIFIED 而在req ...
- array to object
array to object native js & ES6 https://stackoverflow.com/questions/4215737/convert-array-to-obj ...
- [剑指Offer] 58.对称的二叉树
题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. [思路]递归,关键是isSame函数中的最后一句 /* struct Tree ...
- Java SE1.6中的Synchronized
1 引言 在多线程并发编程中Synchronized一直是元老级角色,很多人都会称呼它为重量级锁,但是随着Java SE1.6对Synchronized进行了各种优化之后,有些情况下它并不那么重了,本 ...
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ3977:Subset——题解(三分+折半搜索)
http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...
- BZOJ5248:[九省联考2018]一双木棋——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5248 https://www.luogu.org/problemnew/show/P4363#su ...
- BZOJ1208:[HNOI2004]宠物收养所——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1208 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物 ...
- ACE线程管理机制-面向对象的线程类ACE_Task
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/05/583231.html 我们在前一章中使用ACE_Thread包装时,你一定已经注意到了一 ...