由于k只有2,所以我们分类讨论
显然当k=1时,我们只要连一条最长的路径即可就是树的直径L
少走了L-1条边
如果k=2时,我们再次连边成环后
如果成环路径与上一次的最长路径没有相同的边,那少走的边数是路径长l-1
如果有相同的边,那么相同的边一共还是会走两次,少走的边数是l-1-2*same
因此我们只要把第一次找的的最长路径上的边标记为-1,再做一次树形dp即可

 type node=record
po,len,next:longint;
end; var w:array[..] of node;
f,p1,p2,p:array[..] of longint;
v:array[..] of boolean;
i,ans,loc,maxx,t,n,k,x,y:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y:longint);
begin
inc(t);
w[t].po:=y;
w[t].len:=;
w[t].next:=p[x];
p[x]:=t;
end; procedure dfs(x:longint);
var i,y,s1,s2:longint;
begin
i:=p[x];
v[x]:=true;
s1:=;
s2:=;
while i<>- do
begin
y:=w[i].po;
if not v[y] then
begin
dfs(y);
if f[y]+w[i].len>s1 then
begin
s2:=s1;
p2[x]:=p1[x];
s1:=f[y]+w[i].len;
p1[x]:=i;
end
else if (s2<f[y]+w[i].len) then
begin
s2:=f[y]+w[i].len;
p2[x]:=i;
end;
end;
i:=w[i].next;
end;
// writeln(x,' ',s1,' ',s2);
f[x]:=s1;
if maxx<s1+s2 then
begin
maxx:=s1+s2;
loc:=x;
end;
end; begin
t:=-;
fillchar(p,sizeof(p),);
fillchar(p1,sizeof(p1),);
fillchar(p2,sizeof(p2),);
readln(n,k);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
dfs();
ans:=*(n-)-maxx+;
if k= then
begin
maxx:=;
w[p1[loc]].len:=-;
w[p2[loc]].len:=-;
x:=w[p1[loc]].po;
while p1[x]<>- do
begin
w[p1[x]].len:=-;
x:=w[p1[x]].po;
end;
x:=w[p2[loc]].po;
while p1[x]<>- do
begin
w[p1[x]].len:=-;
x:=w[p1[x]].po;
end;
fillchar(v,sizeof(v),false);
fillchar(f,sizeof(f),);
dfs();
ans:=ans-maxx+;
end;
writeln(ans);
end.
05-02 19:59