f[i,0] 表示 第i个人不参加舞会
f[i,1] 表示 第i个人参加舞会
f[i,1]=sigma(f[j,0])+v[i] j 为 i 的孩子
f[i,1]=sigma(max(f[j,0],f[j,1])) j 为 i 的孩子
ans=max(f[root,0],f[root,1])
Program CODEVS1380; type arr=record u,v,next:longint; end; const maxn=10000; var eg:array[0..maxn] of arr; last:array[0..maxn] of longint; a:array[0..maxn] of longint; f:array[0..maxn,0..1] of longint; fa:array[0..maxn] of longint; i,n,u,v,j,root:longint; procedure add(u,v:longint); begin inc(j); eg[j].u:=u; eg[j].v:=v; eg[j].next:=last[u]; last[u]:=j; end; function max(a,b:longint):longint; begin if a>b then exit(a) else exit(b); end; procedure dfs(u:longint); var i,v,sum1,sum2:longint; begin if last[u]=-1 then begin f[u,0]:=0; f[u,1]:=a[u]; exit; end; i:=last[u]; sum1:=0; sum2:=0; while i<>-1 do begin v:=eg[i].v; dfs(v); sum1:=sum1+f[v,0]; sum2:=sum2+max(f[v,0],f[v,1]); i:=eg[i].next; end; f[u,0]:=sum2; f[u,1]:=sum1+a[u]; end; begin readln(n); j:=-1; for i:=1 to maxn do last[i]:=-1; for i:=1 to n do readln(a[i]); readln(v,u); while (v<>0) and (u<>0) do begin fa[v]:=u; add(u,v); readln(v,u); end; for i:=1 to n do if fa[i]=0 then root:=i; dfs(root); writeln(max(f[root,0],f[root,1])); end.
时间: 2024-09-29 19:38:28