[BZOJ2060] [Usaco2010 Nov]Visiting Cows 拜访奶牛

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=2060

题目大意

给定一棵树,每条边只能访问一个端点,询问最大访问

题解

基本树形DP

var
 dp:array[0..50005,0..1]of longint;
 x:array[0..50005]of longint;
 w:array[0..150005,1..2]of longint;
 i,j,k:longint;
 n,len,a,b,tt:longint;
procedure init(a,b:longint);
begin
 w[len,1]:=b;
 if w[a,2]=0
 then w[a,2]:=len else w[w[a,1],2]:=len;
 w[a,1]:=len; inc(len);
end;

function max(a,b:longint):longint;
begin
 if a>b then exit(a) else exit(b);
end;

procedure dfs(a:longint);
var tt:longint;
begin
 x[a]:=1; tt:=w[a,2]; dp[a,1]:=1; dp[a,0]:=0;
 while tt<>0 do
  begin
   if x[w[tt,1]]=0 then begin
    dfs(w[tt,1]);
    dp[a,1]:=dp[a,1]+dp[w[tt,1],0];
    dp[a,0]:=dp[a,0]+max(dp[w[tt,1],1],dp[w[tt,1],0]);
   end;
   tt:=w[tt,2];
  end;
end;

begin
 readln(n); len:=n+1;
 for i:=1 to n-1 do
  begin
   readln(a,b);
   init(a,b); init(b,a);
  end;
 dfs(1);
 writeln(max(dp[1,1],dp[1,0]));
end.

你可能感兴趣的:([BZOJ2060] [Usaco2010 Nov]Visiting Cows 拜访奶牛)