标签:
1 /************************************************************** 2 Problem: 2060 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:112 ms 7 Memory:2576 kb 8 ****************************************************************/ 9 10 type 11 point=^node; 12 node=record 13 g:longint; 14 next:point; 15 end; 16 vec=record 17 a0,a1:longint; 18 end; 19 var 20 i,j,k,l,m,n:longint; 21 a:array[0..100000] of point; 22 b:array[0..100000] of longint; 23 t:vec; 24 function max(x,y:longint):longint;inline; 25 begin 26 if x>y then max:=x else max:=y; 27 end; 28 procedure add(x,y:longint);inline; 29 var p:point; 30 begin 31 new(p);p^.g:=y; 32 p^.next:=a[x];a[x]:=p; 33 end; 34 function dfs(x:longint):vec;inline; 35 var p:point;t,v:vec; 36 begin 37 b[x]:=1; 38 p:=a[x]; 39 t.a0:=0;t.a1:=1; 40 while p<>nil do 41 begin 42 if b[p^.g]=0 then 43 begin 44 v:=dfs(p^.g); 45 t.a1:=t.a1+v.a0; 46 t.a0:=t.a0+max(v.a1,v.a0); 47 end; 48 p:=p^.next; 49 end; 50 exit(t); 51 end; 52 begin 53 readln(n); 54 for i:=1 to n do a[i]:=nil; 55 fillchar(b,sizeof(b),0); 56 for i:=1 to n-1 do 57 begin 58 readln(j,k); 59 add(j,k);add(k,j); 60 end; 61 t:=dfs(1); 62 writeln(max(t.a0,t.a1)); 63 end.
2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4298398.html