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:先求直径,然后 ...
随机推荐
- nuget程序包还原失败:未能解析此远程名称
一个简便的方法就是取消下载缺少的程序包. 步骤如下: 1,工具--NuGet程序包管理器--程序包管理器设置 2,NuGet Package Manager--常规,取消勾选.
- 网络编程--System.Net
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 使用tc来控制网络流量
https://blog.csdn.net/qinyushuang/article/details/46611709 tc实际操控网络的流量 解释网络tc的架构,从架构上分析tc,与netfilter ...
- Microsoft Edge goes Chromium
Microsoft Edge goes Chromium https://techcrunch.com/2018/12/06/microsoft-edge-goes-chromium-and-maco ...
- [C/C++] C++抽象类
转自:http://www.cnblogs.com/dongsheng/p/3343939.html 一.纯虚函数定义 纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己 ...
- Bootstrap 基本模板理解
<!-- 声明文档类型 为 html5 --> <!DOCTYPE html> <!-- 声明页面内容主要为 中文简体 --> <html lang=&quo ...
- Mybatis笔记二
一对一查询 案例:查询所有订单信息,订单信息中显示下单人信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...
- BZOJ 1407 Savage(拓展欧几里得)
这题的时间复杂度真玄学... O(m*n^2).1e8也能过啊... 首先题目保证m<=1e6. 这启发我们枚举或者二分答案? 但是答案不满足单调性,考虑从小到大枚举m. 对于每一个m,枚举两个 ...
- 去除安卓手机select下拉框默认箭头
-webkit-appearance: listbox;
- CSS-posiziton
1. 想要实现,”返回顶部”永远位于页面的右下角.需要用到position函数.CSS:层叠样式表.用到了分层的功能. position:fixed; 永远固定在一个地方. <!DOCTYPE ...