[codevs1947]道路修建 搜索、树p

不明白为啥一定有一个点re,而同样的c++用int就能跑过(吃屎了有木有)

此刻我内心是崩溃的= =

而且在bz和code上,两个程序跑出来的结果也是不一样的,一个re一个a了

先发一个95分的吧,到时候我再查一下

        var
                pre,last,other,long:array[0..2002333] of qword;
                k,l,n,tot,x,y,z:qword;
                ans,d:qword;
                i,j:longint;

                procedure add(x,y,z:qword);
                begin
                        inc(tot);
                        pre[tot]:=last[x];
                        last[x]:=tot;
                        other[tot]:=y;
                        long[tot]:=z;
                end;

                function dfs(x,p:qword):qword;
                var t,i,v,size:qword;
                begin
                        i:=1;
                        v:=1;
                        size:=1;
                        i:=last[x];
                        while i<>0 do
                        begin
                                v:=other[i];
                                if v=p then begin i:=pre[i]; continue; end;
                                t:=dfs(v,x);
                                inc(size,t);
                                if n>2*t then d:=n-2*t else d:=2*t-n;
                                inc(ans,d*long[i]);
                                i:=pre[i];
                        end;
                        dfs:=size;
                end;

                begin
                        readln(j);
                        for i:=1 to j-1 do
                        begin
                                readln(x,y,z);
                                add(x,y,z);
                                add(y,x,z);
                        end;
                        n:=j;
                        dfs(1,0);
                        writeln(ans);
                end.

 

posted @ 2015-09-24 21:30  ROLL-THE-FIRST  阅读(125)  评论(0编辑  收藏  举报