原题网址:http://www.lydsy.com/JudgeOnline/problem.php?id=1596
有点像“没有上司的舞会”,但因为两座塔可以距离最多为3,所以要记三种状态。
f[x]
表示
x
这个点已经放了,
type
edge=record
y,next:longint;
end;
const
INF=10001;
var
map:array[0..20050] of edge;
f,g,h,first:array[0..10050] of longint;
n,i,x,y,s:longint;
function min(a,b:longint):longint;
begin if (a<b) then exit(a) else exit(b); end;
procedure ins(x,y:longint);
begin
inc(s);map[s].y:=y;
map[s].next:=first[x];first[x]:=s;
end;
procedure dfs(x,fa:longint);
var
t,det,y:longint;
put:boolean;
begin
f[x]:=1;g[x]:=0;h[x]:=0;
t:=first[x];
det:=INF;put:=false;
while (t>0) do
begin
y:=map[t].y;
if (y<>fa) then
begin
dfs(y,x);
inc(f[x],min(min(f[y],g[y]),h[y]));
if (f[y]<=g[y])
then
begin
put:=true;
inc(g[x],f[y]);
end
else
begin
inc(g[x],g[y]);
det:=min(det,f[y]-g[y]);
end;
inc(h[x],g[y]);
end;
t:=map[t].next;
end;
if (not put)
then g[x]:=g[x]+det;
end;
begin
read(n);
for i:=1 to n-1 do
begin
read(x,y);
ins(x,y);
ins(y,x);
end;
dfs(1,0);
writeln(min(f[1],g[1]));
end.