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 ...
 
随机推荐
- js跳转页面方法(转)
			
<span id="tiao">3</span><a href="javascript:countDown"></a& ...
 - CSS 三角形绘制方法
			
#triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 5 ...
 - 使用扩展方法(this 扩展类型)
			
namespace ConsoleApp_UseExtendWays{ class Program { static void Main(string[] args) { Student s = ne ...
 - dig out secrets beneath AirSig
			
My sister installed AirSig last week. She is so exciting about this new techknology and she won't st ...
 - 一个完整的菜谱客户端(android源码)(有独立后台)
			
该源码是自己写的,是一个完整的菜谱类客户端.功能简单比较简单,界面比较丑,自己乱拼接的,只为学习用.功能相对完整,数据来自独立后台,通过http协议获取,全部来自真实数据.代码里面有获取数据的相应ur ...
 - POJ C++程序设计  编程题#4 字符串操作
			
编程题#4: 字符串操作 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 给 ...
 - Windows 2003 FastCgi安装环境
			
Windows 2003 IIS+PHP5.4.3 安装教程 一.准备相关组件 安装前,先安装IIS. 1.安装FastCgi for IIS6 Fastcgi官方网址是:http://www.iis ...
 - VS2008调试快捷键
			
F5: 启动调试 Ctrl+F5: 开始执行(不调试) F10: 逐过程(不进入函数单步) F11: 逐语句(进入函数单步) Shift+F11跳出(实用) Ctrl+F10: 运行到光标处 F6: ...
 - [leetcode]_Container With Most Water
			
题目:在二维坐标系下,有很多个挡板,有两个挡板之间能够积蓄的水的最大面积.如下图所示: 思路:我只想到暴力解法,用O(n2)的时间复杂度算出任意两个挡板形成的面积,这必须的过不了. 优化解法:O(n) ...
 - 在SQL中取出字符串中数字部分或在SQL中取出字符部分
			
在SQL中取出字符串中数字部分或在SQL中取出字符部分 编写人:CC阿爸 2013-10-18 近来在开发一个项目时,一包含数字的字符串,需要取出中间的数字部分进行排序.经过baidu搜索.并结合自己 ...