【BZOJ】【1912】【APIO2010】patrol巡逻
树形DP
说是树形DP,其实就是求树的最长链嘛……
K=1的时候明显是将树的最长链的两端连起来最优。
但是K=2的时候怎么搞?
考虑第一次找完树的最长链以后的影响:第一次找过的边如果第二次再走,对答案的贡献会变成-1,因为两次都选这一段的话,反而会使得这一段不得不走两次(如果只被选一次的话就可以只走一次),所以就将第一次找出的树的最长链上的边权值都改为-1。这个操作可以用链表实现(类比一下最小费用最大流的spfa实现!)
题解:http://blog.csdn.net/qpswwww/article/details/43935861
/**************************************************************
Problem: 1912
User: Tunix
Language: C++
Result: Accepted
Time:580 ms
Memory:5752 kb
****************************************************************/ //BZOJ 1912
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
#define CC(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=1e5+,INF=~0u>>;
typedef long long LL;
/******************tamplate*********************/
int head[N],to[N<<],next[N<<],len[N<<],cnt=;
void add(int x,int y){
to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt; len[cnt]=;
to[++cnt]=x; next[cnt]=head[y]; head[y]=cnt; len[cnt]=;
}
int n,K;
int D,S,son1[N],son2[N];
int dfs(int x,int fa){
int m1=,m2=;
for(int i=head[x];i;i=next[i])
if (to[i]!=fa){
int nowh=dfs(to[i],x)+len[i];
if (nowh>m1){m2=m1;m1=nowh;son2[x]=son1[x];son1[x]=i;}
else if (nowh>m2){m2=nowh; son2[x]=i;}
}
if (D<m1+m2) D=m1+m2,S=x;
return m1;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1912.in","r",stdin);
freopen("1912.out","w",stdout);
#endif
n=getint(); K=getint();
F(i,,n){
int x=getint(),y=getint();
add(x,y);
}
int ans=(n-)<<;
D=;
CC(son1,-);
CC(son2,-);
dfs(,-);
ans-=D-;
if (K>){
D=;
for(int i=son1[S];i!=-;i=son1[to[i]]) len[i]=len[i^]=-;
for(int i=son2[S];i!=-;i=son1[to[i]]) len[i]=len[i^]=-;
CC(son1,-);
CC(son2,-);
dfs(,-);
ans-=D-;
}
printf("%d\n",ans);
return ;
}
1912: [Apio2010]patrol 巡逻
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 642 Solved: 362
[Submit][Status][Discuss]
Description
Input
表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n)。
Output
Sample Input
1 2
3 1
3 4
5 3
7 5
8 5
5 6
Sample Output
HINT
10%的数据中,n ≤ 1000, K = 1;
30%的数据中,K = 1;
80%的数据中,每个村庄相邻的村庄数不超过 25;
90%的数据中,每个村庄相邻的村庄数不超过 150;
100%的数据中,3 ≤ n ≤ 100,000, 1 ≤ K ≤ 2。
Source
【BZOJ】【1912】【APIO2010】patrol巡逻的更多相关文章
- BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...
- bzoj 1912 : [Apio2010]patrol 巡逻 树的直径
题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...
- bzoj 1912: [Apio2010]patrol 巡逻【不是dp是枚举+堆】
我是智障系列.用了及其麻烦的方法= =其实树形sp就能解决 设直径长度+1为len(环长) 首先k=1,直接连直径两端就好,答案是2*n-len 然后对于k=2,正常人的做法是树形dp:先求直径,然后 ...
- bzoj 1912: [Apio2010]patrol 巡逻
呵呵呵呵呵呵,自己画图,大概半个小时,觉的连上边会成环(是不是该交仙人掌了??)然后求环不重合部分最大就好了, 结果写了一坨DP,最后写不下去了,再次扒了题解. 发现我真的是个sb. k==1,直接是 ...
- 【BZOJ】1912: [Apio2010]patrol 巡逻(树的直径)
题目 传送门:QWQ 分析 $ k=1 $ 时显然就是树的直径 $ k=2 $ 时怎么做呢? 做法是把一开始树的直径上的边的边权改成$ -1 $,那么当我们第二次用这些边做环时就抵消了一开始的贡献. ...
- BZOJ 1912:[Apio2010]patrol 巡逻(树直径)
1912: [Apio2010]patrol 巡逻 Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ ...
- [Apio2010]patrol 巡逻
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2541 Solved: 1288[Submit][S ...
- 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP
[BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...
- 【bzoj1912】 Apio2010—patrol 巡逻
http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...
- BZOJ1912 [Apio2010]patrol 巡逻
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
随机推荐
- (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- 有关c#装箱和拆箱知识整理
c#装箱和拆箱知识,装箱和拆箱是一个抽象的概念. 1.装箱和拆箱是一个抽象的概念 2.装箱是将值类型转换为引用类型 : 拆箱是将引用类型转换为值类型 利用装箱和拆箱功能,可通过允许值类型的任何值与O ...
- 带有×的EditText
代码: EditTextWithDel.java(直接复制): package com.sunday.customs; import com.example.customs.R; import and ...
- PHP 把GBK编码转换为UTF8
//把GBK编码转换为UTF8 $name="勿以善小而不为"; $name=iconv("GBK", "UTF-8", $name);
- NuGet 的使用
install-package entityframework//Enable-Migrations -ContextTypeName College.Models.CollegeEntities ...
- 009-python基础-数据类型-列表和元组
一.列表 在python中叫"列表",其他语言中成为"数组" 元素中可以存储字符串.数字甚至变量. 元素索引顺序从0开始. 例如 name_list[0] 就是 ...
- python的http请求应用--每日签到
写点python吧,python其实是个很好用的工具,作为浇水语言,跟其他语言联系也很紧密,想用什么包直接import,导入ctypes调用底层函数库,导入web相关的包可以轻松写爬虫,今天我们写的跟 ...
- 解决Unable to resolve superclass的问题
之前在GITHUB上看到大蛋的高级设置,昨晚于是就拿来编译.. 结果真是个悲伤的故事,放手机上居然运行不了,这种时候只能看LOG了! 看LOG得出的信息如下: - ::): threadid=: in ...
- android----Java DES加密算法工具类
DESUtil类 public class DESUtil { private static byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (by ...
- [quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming
[quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming http://blog.pi3g.com/2013/08/ffmp ...