P1912: [Apio2010]patrol 巡逻

时间:2021-08-29 01:01:56

这道题讨论了好久,一直想不明白,如果按传统的随便某一个点出发找最长链,再回头,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/)