P1912: [Apio2010]patrol 巡逻
这道题讨论了好久,一直想不明白,如果按传统的随便某一个点出发找最长链,再回头,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 巡逻的更多相关文章
- 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP
[BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...
- 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 巡逻
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- 【树形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 ...
- 【bzoj1912】 Apio2010—patrol 巡逻
http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...
- bzoj 1912 : [Apio2010]patrol 巡逻 树的直径
题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...
- BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...
随机推荐
- 009Linux密码故障排除
1.Root密码破解/忘记Root密码: 步骤: (1)在系统启动时进入grub选项菜单: 在系统开机读秒时,按回车键,注意,要迅速,读秒的时间很快,但还需注意的是,虽然需要迅速,但是只按一次回车键就 ...
- 1.7见识一下什么叫Linux驱动:LED
1.任何的Linux驱动都有一个装载函数(装载驱动时调用)和一个卸载函数(卸载驱动时调用): 2.装载函数和卸载函数分别通过module_init和module_exit宏指定.
- 查看cics 运行状态
查看cics 运行状态cicscp -v status all
- centos下网络的基本配置方法讲解
上一篇中我们已经成功安装了我们的centos系统,但是我们可能发现我们安装的centos上不了网,所以这一章我们来说说如何配置centos来连接外网和局域网. 我们首先来认识一下linux下部分网络配 ...
- 【SMS】移动短信网关返回信息状态代码说明【China Mobile】
1 由SMSC返回的一般结果状态报告 含义 说明 处理建议DELIVRD 消息发送成功 用户成功接收到短信 ??EXPIRED 因为用户长时间关机或者不在服务区等导致的短消息超时没有递交到用户手机上 ...
- Asp.net从文件夹中读取图片,随机背景图
第一步:配置文件web.config里添加 <system.web><connectionStrings> <!--name 是自定义的,connectionString ...
- 通过Eclipse创建SQLite数据库
import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database ...
- 13)Java static
1.static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是: ...
- spring mvc中的文件上传
使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...
- DevExpress控件使用经验总结- GridView列表行号显示操作
DevExpress是一个比较有名的界面控件套件,提供了一系列的界面控件套件的DotNet界面控件.本文主要介绍我在使用DevExpress控件过程中,遇到或者发现的一些问题解决方案,或者也可以所示一 ...