Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这是我的代码,能帮我看看为什么WA吗?

Posted by 1139784495 at 2017-01-26 23:02:18 on Problem 1741
Const maxn=10005;
Var n,m,u,v,i,j,k,t,ans,ans1:longint;
    first,size,dep,dis:array[0..maxn] of longint;
    next,ot,len:array[0..maxn*2] of longint;
    b,used:array[0..maxn] of boolean;

Procedure solve(p,d1,d2:longint);
Var w,num,k,v,reans,reans1:longint;
    wtf:boolean;

  Procedure dfssize(u:longint);
  Var v,k:longint;
  Begin
    size[u]:=1;
    b[u]:=true;
    k:=first[u];
    while k<>-1 do begin
      v:=ot[k];
      if not b[v] and not used[v] then begin
        dfssize(v);
        size[u]:=size[u]+size[v];
      end;
      k:=next[k];
    end;
  End;

  Procedure getw(u,n:longint);
  Var v,k:longint;
      f:boolean;
  Begin
    if wtf then exit;
    b[u]:=true;
    k:=first[u];
    f:=false;
    if n-size[u]<=n div 2 then f:=true;
    while k<>-1 do begin
      v:=ot[k];
      if not b[v] and not used[v] then begin
        if size[v]>n div 2 then f:=false;
        getw(v,n);
      end;
      k:=next[k];
    end;
    if f then begin
      w:=u;  wtf:=true;
    end;
  End;

  Procedure dfsdepth(u:longint);
  Var k,v:longint;
  Begin
    b[u]:=true;
    k:=first[u];
    while k<>-1 do begin
      v:=ot[k];
      if not b[v] and not used[v] then begin
        dep[v]:=dep[u]+len[k];
        dfsdepth(v);
      end;
      k:=next[k];
    end;
    inc(num); dis[num]:=dep[u];
  End;

  Procedure Qs(l,r:longint);
  Var i,j,x,t:longint;
  Begin
    i:=l; j:=r; x:=Dis[(i+j) div 2];
    Repeat
      while dis[i]<x do inc(i);
      while dis[j]>x do dec(j);
      if i<=j then begin
        t:=dis[i];
        dis[i]:=dis[j];
        dis[j]:=t;
        inc(i); dec(j);
      end;
    Until i>j;
    if l<j then Qs(l,j);
    if i<r then Qs(i,r);
  End;

  Procedure calc(d1,d2:longint);
  Var i,j:longint;
  Begin
    ans:=0; ans1:=0;
    i:=1; j:=num;
    while i<j do
      if dis[i]+dis[j]<=d1 then begin
        ans:=ans+j-i;
        inc(i);
      end
      else dec(j);
    i:=1; j:=num;
    while i<j do
      if dis[i]+dis[j]<=d2 then begin
        ans1:=ans1+j-i;
        inc(i);
      end
      else dec(j);
  End;

Begin
  fillchar(b,sizeof(b),0);
  dfssize(p);
  fillchar(b,sizeof(b),0);
  wtf:=false;
  getw(p,size[p]);
  used[w]:=true;
  dep[w]:=0;    num:=0;
  fillchar(b,sizeof(b),0);
  dfsdepth(w);
  Qs(1,num);
  calc(d1,d2);
  reans:=ans; reans1:=ans1;
  k:=first[w];
  while k<>-1 do begin
    v:=ot[k];
    if not used[v] then begin
      solve(v,d1,d1-2*len[k]);
      reans:=reans+ans-ans1;
    end;
    k:=next[k];
  end;
  ans:=reans; ans1:=reans1;
End;

Begin
  readln(n,t);
  while not ((n=0)and(t=0)) do begin
  for i:=0 to n do begin
    first[i]:=-1; used[i]:=false;
  end;
  m:=n-1;
  for i:=1 to m do begin
    read(u,v);
    next[i]:=first[u];
    first[u]:=i;
    ot[i]:=v;
    readln(len[i]);
    next[i+m]:=first[v];
    first[v]:=m+i;
    ot[m+i]:=u;
    len[m+i]:=len[i];
  end;
  solve(1,t,t);
  writeln(ans);
  readln(n,t);
  end;
End.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator