比赛 2009noip模拟试卷 评测结果 AAAAAAAAAA
题目名称 火车站饭店 最终得分 100
用户昵称 doriko 运行时间 0.525 s
代码语言 Pascal 内存使用 4.38 MiB
提交时间 2016-10-09 14:33:47
显示代码纯文本
uses math;
var  dp:Array[0..100000,0..1]of longint;
     rd:array[0..200000,1..3]of longint;
     fa,last,w,f:array[0..100000]of longint;
     i,j,x,y,n,num:longint;


procedure dfs(x:longint);
var next:longint;t:boolean;
  begin
    next:=last[x];f[x]:=1;
    t:=false;

    while next<>0 do
      begin
        if f[rd[next,2]]=1 then
           begin
             next:=rd[next,3];
             continue;
           end;
        t:=true;
        fa[rd[next,2]]:=x;
        dfs(rd[next,2]);
        inc(dp[x,1],dp[rd[next,2],0]);
        inc(dp[x,0],max(dp[rd[next,2],0],dp[rd[next,2],1]));

        next:=rd[next,3];
      end;

  end;

  begin

    assign(input,'profitz.in');reset(input);
    assign(output,'profitz.out');rewrite(output);

    readln(n);
    for i:=1 to n do read(w[i]);

    for i:=1 to n-1 do
        begin
          read(x,y);
          inc(num);rd[num,1]:=x;rd[num,2]:=y;rd[num,3]:=last[x];last[x]:=num;
          inc(num);rd[num,1]:=y;rd[num,2]:=x;rd[num,3]:=last[y];last[y]:=num;
        end;
    for i:=1 to n do dp[i,1]:=w[i];
    dfs(1);

    writeln(max(dp[1,0],dp[1,1]));

    close(output);

  end.