这道题讨论了好久,一直想不明白,如果按传统的随便某一个点出发找最长链,再回头,K=2 的时候赋了-1就没法用这种方法找最长链了,于是乎,更强的找最长链的方法就来了。。类似于DP的东西吧。先上代码:

 const maxn=;
type
node=record
f,t,l:longint;
end;
var n,k,i,j,ans,num,f,t,diameter,s,sum:longint;
b:array[..*maxn] of node;
head,go1,go2:array[..maxn] of longint;
procedure swap(var a,b:longint);
var tem:longint;
begin
tem:=a; a:=b; b:=tem;
end;
procedure insert(num,f,t:longint);
begin
b[num].f:=head[f];
b[num].t:=t;
b[num].l:=;
head[f]:=num;
end;
function dfs(x,f:longint):longint;
var nowe,max1,max2,tem:longint;
begin
max1:=; max2:=; //tem:=0;
nowe:=head[x];
while nowe<> do
begin
if b[nowe].t=f then
begin
nowe:=b[nowe].f;
continue;
end;
tem:=dfs(b[nowe].t,x)+b[nowe].l;
if tem>max1 then begin
go2[x]:=go1[x];
go1[x]:=nowe;
max2:=max1;
max1:=tem;
end
else if tem>max2 then
begin
go2[x]:=nowe;
max2:=tem;
end;
nowe:=b[nowe].f;
end;
if diameter<max1+max2 then
begin
diameter:=max1+max2;
s:=x;
end;
exit(max1);
end;
begin
readln(n,k);
for i:= to n- do
begin
readln(f,t);
insert(i*-,f,t);
insert(i*,t,f);
end;
ans:=*(n-);
diameter:=;
sum:=dfs(,);
ans:=ans-diameter+;
if k> then
begin
diameter:=;
i:=go1[s];
while i<> do
begin
b[i].l:=-;
i:=go1[b[i].t];
end;
i:=go2[s];
while i<> do
begin
b[i].l:=-;
i:=go1[b[i].t];
end;
t:=dfs(,);
ans:=ans-diameter+;
end;
writeln(ans);
end.
 int diameter,s; //树的直径为diameter,直径的起点是s
int son1[MAXV],son2[MAXV]; //记录最长路与次长路的路径 int DFS(int u,int fa)
{
int max1=,max2=; //与当前点相连的最长路与次长路 之和为不过fa的最长链,或者与fa相连并 //加上与fa相连边权值,作为连接fa可能的max1 或 max2
for(int p=head[u];p!=-;p=edges[p].next)
{
int v=edges[p].v;
if(v==fa) continue; //一直往下走,直到叶子节点
int nowh=DFS(v,u)+edges[p].w;
if(nowh>max1) max2=max1,son2[u]=son1[u],max1=nowh,son1[u]=p;
else if(nowh>max2) max2=nowh,son2[u]=p;
}
if(diameter<max1+max2) diameter=max1+max2,s=u;
return max1;
}

而复杂度也是 O(n)的。

(转载请注明出处:http://www.cnblogs.com/Kalenda/)

P1912: [Apio2010]patrol 巡逻的更多相关文章

  1. 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP

    [BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...

  2. BZOJ 1912:[Apio2010]patrol 巡逻(树直径)

    1912: [Apio2010]patrol 巡逻 Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ ...

  3. [Apio2010]patrol 巡逻

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2541  Solved: 1288[Submit][S ...

  4. BZOJ1912 [Apio2010]patrol 巡逻

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  5. 【树形dp 最长链】bzoj1912: [Apio2010]patrol 巡逻

    富有思维性的树形dp Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, ...

  6. BZOJ1912:[APIO2010]patrol巡逻

    Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...

  7. 【bzoj1912】 Apio2010—patrol 巡逻

    http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...

  8. bzoj 1912 : [Apio2010]patrol 巡逻 树的直径

    题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...

  9. BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...

随机推荐

  1. EasyUI学习笔记

    1,tabs获得被选中的标题 var tabTitle = $('#tabs').tabs('getSelected').panel('options').title;//获得被选中的标题 2.当设置 ...

  2. angular.foreach 循环方法使用指南

    angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” },{a:}]; angular.forEach ...

  3. java语言基础02

    一.Java语言基础(常量的概述和使用)(掌握) 1:什么是常量 就是在程序的执行过程中其值不发生改变的量. 2:Java中常量的分类 (1):字面值常量 (2):自定义常量(面向对象部分讲解) 3: ...

  4. 【项目相关】MVC中使用WebUploader进行图片预览上传以及编辑

    项目中需要用到多图片上传功能,于是在百度搜了一下,首先使用了kissy uploader,是由阿里前端工程师们发起创建的一个开源 JS 框架中的一个上传组件...但,后面问题出现了. 在对添加的信息进 ...

  5. always pick the choice that scares you a little

    “One of my philosophies is to always pick the choice that scares you a little. The status quo, the p ...

  6. 设置DataGridView 显示自己添加编辑的列名,不动态显示数据库本身

    设置DataGridView 显示自己添加编辑的列名,不动态显示数据库本身. 方法: (1)界面操作,把DataGridView控件拖放在窗体中,就看到DataGridView控件的右上角有个小三角, ...

  7. js----全局变量和局部变量部分讲解

    以此文作为自己学习的一个总结. 关于全局变量和局部变量的一句简单的定义:在函数外声明的变量都为全局变量,在函数内声明的为局部变量. 一.局部变量和全局变量重名会覆盖全局变量 var a = 1; fu ...

  8. c语言学习第四天数据类型1

    int   代表整数,它在内存中占4个字节,二进制的表示方式是占用了三十二位,二进制中只包含0和1,那它的最大值就是全为1,但int是 有符号类型,所以最高位(左边的第一位)要拿出来做符号位,这样就只 ...

  9. Laravel 5 基础(十)- 日期,Mutator 和 Scope

    在我们前面的解决方案中,直接给 published_at 赋值为当前日期实际上是一个临时解决方案,我们需要设定发布日期,可能是未来2天后才发布,让我们修改这个问题. 首先修改控制器: public f ...

  10. svn的使用!!!

    1.SVN:subversion(子级源代码版本控制管理软件) 2.SVN的作用 (1)避免开发同一项目不会出现代码覆盖. (2)同一文件可以创建许多不同的版本,并可以随时查看不同版本的内容. (3) ...