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

这题分明欺负PASCAL的------内容完全一样的两个程序中只有C++的能过。

Posted by cqf at 2006-05-28 21:19:12 on Problem 2827
PASCAL:
program p2827(input,output);
const maxh=54117;
var h,y:array[0..maxh] of longint;
    f,len:array[1..20000] of longint;
    fa,fb,i,m,n,a,b,c,tmp:longint;
function hash(r:longint):longint;
var i:longint;
begin
   i:=r mod maxh;
   while (h[i]<>-1)and(h[i]<>r) do
      i:=(i+1)mod maxh;
   if h[i]=-1 then begin
      h[i]:=r;
      inc(m);
      y[i]:=m;
   end;
   hash:=y[i];
end;
function father(r:longint):longint;
var tmp:longint;
begin
   if f[r]<=0 then
      father:=r
   else begin
      tmp:=f[r];
      f[r]:=father(f[r]);
      inc(len[r],len[tmp]);
      father:=f[r];
   end;
end;
begin
   while not seekeof(input) do begin
      readln(n);
      m:=0;
      fillchar(h,sizeof(h),255);
      fillchar(f,sizeof(f),0);
      fillchar(len,sizeof(len),0);
      for i:=1 to n do begin
         readln(a,b,c);
         if a>b then begin
            tmp:=a;
            a:=b;
            b:=tmp;
         end;
         a:=hash(a-1);
         b:=hash(b);
         fa:=father(a);
         fb:=father(b);
         if fa=fb then
            if len[a]-len[b]=c then
               writeln('Accept')
            else
               writeln('Bug Detected ',len[a]-len[b])
         else begin
            writeln('Accept');
            f[fa]:=fb;
            len[fa]:=c-(len[a]-len[b]);
         end;
      end;
   end;
end.

C++(感谢UPDOG为我把PASCAL程序转化C++程序):
#include <stdio.h>
#include <string.h>

const int maxh=54117;
int h[maxh+1],y[maxh+1];
int f[20000+1],len[20000+1];
int fa,fb,i,m,n,a,b,c,tmp;

int hash( int r )
{
   int i=r%maxh;
   while( h[i]!=-1 && h[i]!=r ) i=(i+1)%maxh;
   if( h[i]==-1 ) { h[i]=r; m++; y[i]=m; }
   return y[i];
}

int father( int r )
{
   int tmp;
   if( f[r]<=0 ) return r;
   else
   {
      tmp=f[r]; f[r]=father(f[r]);
      len[r]+=len[tmp];
      return f[r];
   }
}

int main()
{
   while( scanf("%d",&n)!=EOF )
   {
      m=0;
      memset( h, 255, sizeof(h) );
      memset( f, 0, sizeof(f) );
      memset( len, 0, sizeof(len) );
      for( i=1; i<=n; i++ )
      {
         scanf("%d%d%d",&a,&b,&c);
         if( a>b ) { tmp=a; a=b; b=tmp; }
         a=hash(a-1);
         b=hash(b);
         fa=father(a); fb=father(b);
         if( fa==fb )
            if( len[a]-len[b]==c ) puts("Accept");
            else printf("Bug Detected %d\n",len[a]-len[b]);
         else
         {
            puts("Accept");
            f[fa]=fb;
            len[fa]=c-(len[a]-len[b]);
         }
      }
   }
} 

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