这道题讨论了好久,一直想不明白,如果按传统的随便某一个点出发找最长链,再回头,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. python3.5在print和input上的几个变化

    1. 在python3.5中使用print,打印内容必须用括号()括起来.python2.7中可以不用括号,如果你加了括号,代码在python2.7中也是可以正常运行的. python3.5 exam ...

  2. 模仿QQ空间 网页设计

    目的:1.通过模仿QQ空间,全自主写代码,熟悉网页设计的流程 2.熟练的掌握HTML.CSS.JS的应用 3.将在此过程中遇到的问题及其解决方法记录在此,以便取用. 开始: 一.登陆界面(index. ...

  3. JSF 监听

    JSF项目中实现基于RBAC模型的权限管理设计(二) 转 4.3 权限验证模块设计 一个好的权限管理机制在项目中应用时,最好不要让程序员在具体业务代码的方法中来判断用户权限.因为这意味着大量重复的代码 ...

  4. 手机连接wifi自动弹窗的原理及其实现方案

    一.手机连上wifi后会自动弹窗的原理 生活中,有很多需要认证的路由器,手机连接wifi热点后会自动弹出一个网页,让用户输入账号和密码,比如星巴克,肯地基,麦当劳,甚至是火车站和机场的候车室.其实这是 ...

  5. .NET中使用log4net

    一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...

  6. 【转载】linux中互斥尽量用mutex,不用semaphore

    DEFINE_MUTEX是来自include/linux/mutex.h中的一个宏,用它可以定义一把互斥锁,在Linux内核中,其实是在2005年底才建立比较系统.完善的互斥锁机制,在那年冬天,来自R ...

  7. Send User to a Portal Folder

    Sometimes you would want to give users the option to click a button on the page and send them back t ...

  8. python restful 框架之 eve 外网访问设置

    官网地址: http://python-eve.org/ 配合mongodb进行crud使用起来很方便,但是部署的时候遇到一个问题,按照官网和Deom说的,servername使用 '127.0.0. ...

  9. php使用swoole实现一个简单的多人在线聊天群发

    聊天逻辑的好多细节没有实现,只实现群发. php代码: $serv = new swoole_websocket_server("127.0.0.1",3999); //服务的基本 ...

  10. SharePoint ribbon icons disappeared(网站顶部Top bar 齿轮图标,以及编辑模式下Ribbon中Icon消失)

    Questions: has anyone ever seen this before? all my icons in my ribbon have disappeared. I'm using m ...