BZOJ1912 [Apio2010]patrol 巡逻
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description

Input
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。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
const int MAXM = ;
int n,k,ecnt,next[MAXM],to[MAXM],w[MAXM];
int f[MAXN][],first[MAXN],g[MAXN],p[MAXN];
int ans,root,Ans,Son,Son2; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
}
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=; }
inline void dfs(int x,int fa){
int now,son=,son2=;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue; dfs(v,x); now=f[v][]+w[i];
if(now>f[x][]) son=g[x],son2=p[x],f[x][]=f[x][],f[x][]=now,g[x]=v,p[x]=i; else if(now>f[x][]) f[x][]=now,son=v,son2=i;
}
if(f[x][]+f[x][]>ans) { ans=f[x][]+f[x][]; root=x; Son=son; Son2=son2; }
} inline void work(){
n=getint(); k=getint(); int x,y; for(int i=;i<n;i++) { x=getint(); y=getint(); link(x,y); link(y,x); }
dfs(,); Ans=*(n-)-ans+; if(k==) { printf("%d",Ans); return ; }
if(f[root][]>) { x=Son; w[Son2]=-; while(g[x]) { w[p[x]]=-; x=g[x]; } }
x=root; while(g[x]) w[p[x]]=-,x=g[x]; ans=; memset(f,,sizeof(f));
dfs(,); Ans-=ans-; printf("%d",Ans);
} int main()
{
work();
return ;
}
BZOJ1912 [Apio2010]patrol 巡逻的更多相关文章
- 【树形dp 最长链】bzoj1912: [Apio2010]patrol 巡逻
富有思维性的树形dp Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, ...
- BZOJ1912:[APIO2010]patrol巡逻
Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...
- 2018.11.06 bzoj1912: [Apio2010]patrol 巡逻(树形dp)
传送门 一道挺妙的题啊. 对于K==1K==1K==1的直接求树的直径. 对于K==2K==2K==2的先求一次直径,然后考虑到如果两条边加进去形成的两个环重叠就会有负的贡献. 因此把之前那条直径上的 ...
- 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP
[BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...
- 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1034 Solved: 562[Submit][St ...
- 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 巡逻
http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...
- P1912: [Apio2010]patrol 巡逻
这道题讨论了好久,一直想不明白,如果按传统的随便某一个点出发找最长链,再回头,K=2 的时候赋了-1就没法用这种方法找最长链了,于是乎,更强的找最长链的方法就来了..类似于DP的东西吧.先上代码: ; ...
随机推荐
- java多线程系类:基础篇:10生产者消费者的问题
概要 本章,会对"生产/消费者问题"进行讨论.涉及到的内容包括:1. 生产/消费者模型2. 生产/消费者实现 转载请注明出处:http://www.cnblogs.com/skyw ...
- maven spring profile 协同
请在网上查相关的使用情景,这里直接上要点.另外,可能不只一种方法,但这里只有一种. 1.POM.XML片段,使web.xml文件中有关活跃spring profile的内容可以被maven自动替换 & ...
- Android智能指针sp wp详解
研究Android的时候,经常会遇到sp.wp的东西,网上一搜,原来是android封装了c++中对象回收机制.说明:1. 如果一个类想使用智能指针,那么必须满足下面两个条件: a. 该类是虚基 ...
- spring: 加载远程配置
通常在spring应用中,配置中的properties文件,都是打包在war包里的,部署规模较小,只有几台服务器时,这样并没有什么大问题.如果服务器多了,特别是集群部署时,如果要修改某一项配置,得重新 ...
- PHP面向对象编程知识要点
1.基本概念 1.1.面向对象的阶段概念 OOA:面向对象分析 OOD:面向对象设计 OOP:面向对象编程 1.2.类的概念 面向对象中的类,实质上就是现实世界中一类有着相似属性事物抽象的概括,像鸟类 ...
- 文本 To 音频
文本 To 音频 TextToSpeech介绍 TextToSpeech,简称 TTS,是Android 1.6版本中比较重要的新功能.将所指定的文本转成不同语言音频输出.它可以方便的嵌入到游戏或 ...
- JavaScript面试题收集(一)
简述javascript中的“=.==.===”的区别? 答:=赋值 ==比较是否一般相等 "3"==3 //会做类型的隐式转换,true ===比较是否严格相等 " ...
- Excel导入导出,通过datatable转存(篇一)
//导入数据 public ActionResult ExpressInfoImport() { var ptcp = new BaseResponse() { DoFlag = true, DoRe ...
- nios II--实验4——按键中断硬件部分
按键中断 硬件开发 新建原理图 1.打开Quartus II 11.0,新建一个工程,File -> New Project Wizard…,忽略Introduction,之间单击 Next&g ...
- Overlay network 覆盖网络
From Wikipedia, the free encyclopedia An overlay network is a computer network that is built on top ...