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的东西吧.先上代码: ; ...
随机推荐
- SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用
通常在我们的项目中,都会涉及到母版页的定制.并且必不可少的,需要配合以一套自己的JavaScript框架和CSS样式.你有没有遇到过这样的情况呢,在开发环境和UAT时都还算顺利,但是当最终部署到生产服 ...
- codevs 1531 山峰
codevs 1531 山峰 题目描述 Description Rocky山脉有n个山峰,一字排开,从西向东依次编号为1, 2, 3, --, n.每个山峰的高度都是不一样的.编号为i的山峰高度为hi ...
- velocity模板引擎学习(2)-velocity tools 2.0
使用velocity后,原来的很多标签无法使用了,必须借助velocity tools来完成,目前velocity tools最新版本是2.0,下面是velocity tools的一些注意事项: 1. ...
- 【转】aspx与aspx.cs的关系
原文地址: http://www.cnblogs.com/axzxs2001/archive/2009/01/19/1378383.html 在vs中,有很多朋友问起,在一个网站项目中的aspx和as ...
- 【分布式协调器】Paxos的工程实现-cocklebur简介(一)
初识分布式协调器 分布式协调器的“协调”二字让人摸不到头脑,怎么就协调了,用的着协调吗?实际上这个东西在之前就是为了提供分布式锁服务而设计的,伟大的google公司发明了chubby,雅虎随后也推出了 ...
- js异步状态监控
说明:写这篇文章,是希望被吐槽的. 一.背景 在做报表页面的时候,页面上有很多的异步加载,而设计的loading是个全局的,一个页面就有一个. 控制loading什么时候出现,什么时候消失,要实时的知 ...
- EF增删改查操作
增 using (StudentEntities ent = new StudentEntities()) { User aNewUser = new User() { Age = , Name = ...
- 替罪羊树模板(BZOJ1056/1862)
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #defin ...
- javascript 连等赋值问题(这是从SegmentFault转过来的一个问题)
var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);// - ...
- Android回调
当A页面跳往B页面做一些操作后,再从B页面回到A页面时,A页面想要回去一些B页面操作的数据时,我们一般会使用回调. 1 public class MainActivity extends Activi ...