比赛 20121012上午 评测结果 AAAAAAAAAA
题目名称 引爆炸弹 最终得分 100
用户昵称 亟隐 运行时间 2.564 s
代码语言 Pascal 内存使用 4.46 MiB
提交时间 2012-10-12 10:03:41
显示代码纯文本
type    nodd=record
                y,n:longint;
        end;

var     f,a:array[0..201000]of longint;
        b:array[0..201000]of nodd;
        d:array[0..201000]of int64;
        bb:array[0..201000]of boolean;
        n,m,i,j,t,k,tt,s:longint;
        x,sum:int64;

procedure init;
begin
        readln(n,m);
        for i:=1 to n do
        begin
                read(f[i],d[i]);
                b[i].y:=i; b[i].n:=a[f[i]]; a[f[i]]:=i;
        end;
end;

procedure dfs(t,s:longint);
var     p:longint;
begin
        p:=a[t]; d[t]:=d[t]+s; bb[t]:=true;
        while p<>0 do
        begin
                if not bb[b[p].y] then dfs(b[p].y,d[t]);
                p:=b[p].n;
        end;
end;

procedure dfs2(t:longint);
var     p:longint;
begin
        p:=a[t]; d[t]:=d[t]-x;
        while p<>0 do
        begin
                if bb[b[p].y] then dfs2(b[p].y);
                p:=b[p].n;
        end;
end;

procedure work;
begin
        fillchar(bb,sizeof(bb),0);
        k:=1; dfs(0,0); sum:=0;
        for tt:=1 to m do
        begin
                x:=0;j:=0;
                for i:=1 to n do if bb[i] then if d[i]>x then
                begin
                        x:=d[i]; j:=i;
                end;
                k:=-1; sum:=sum+x;
                while j<>0 do
                begin
                        bb[j]:=false; x:=d[j];
                        dfs2(j); j:=f[j];
                end;

        end;
        writeln(sum);
end;

begin
assign(input,'bombb.in');reset(input);
assign(output,'bombb.out');rewrite(output);
        init;
        work;
close(output);
end.