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很费劲

Posted by sunmoonstar_love at 2005-08-23 18:55:43 on Problem 2513
In Reply To:您有空的话帮忙看一下。。。。 Posted by:fujieyun at 2005-08-23 18:51:19
> {$I-,Q-,R-,S-}
> 
> const maxn = 250000+10;
> 
> var a : array [1..maxn] of record
>                              st1, st2 : string[10];
>                              n1, n2   : longint;
>                            end;
>     d, fa, rank : array [1..maxn*2] of longint;
>     name : array [1..600020] of record
>                                   name : string[10];
>                                   code : longint;
>                                 end;
>     n, m, now : longint;
>   
> function findpa( p : longint ): longint;
>   begin
>     if fa[p] = p
>       then findpa := p
>       else begin
>            fa[p] := findpa( fa[p] );
>            findpa := fa[p];
>            end;
>   end;
> 
> procedure union( a,b : longint );
>   begin
>     if rank[a] > rank[b]
>       then fa[b] := a
>       else begin
>            fa[a] := b;
>            if rank[b] = rank[a] then inc( rank[b] );
>            end;
>   end;
>   
> function hash( st : string ): longint;
>   var i, h, g : longint;
>   begin
>     h := 0;
>     for i := 1 to length(st) do
>       begin
>       h := h shl 4 + ord( st[i] );
>       g := h and ($f0000000);
>       if g>0 then h := h xor( g shr 24 );
>       h := h and (not g);
>       end;
>     h := h mod m;
>     hash := h;
>     {h := 0;
>     for i := 1 to length(st) do
>       h := (h*256 + ord( st[i] )) mod m;}
>   end;
>   
> procedure assign_code( st : string[10]; var p : longint );
>   var posi : longint;
>   begin
>     posi := hash( st ) mod m;
>     {writeln( posi );}
>     while (name[ posi ].name <> st)and(name[posi].code<>0) do
>       posi := (posi + 1) mod m;
>     if name[ posi ].code = 0
>       then begin
>            name[ posi ].name := st; name[ posi ].code := now + 1;
>            inc( now ); p := now;
>            end
>       else p := name[ posi ].code;
>   end;
>   
> function prime( p : longint ): boolean;
>   var i : longint;
>   begin
>     prime := false;
>     for i := 2 to trunc( sqrt(p) ) do
>       if p mod i = 0
>         then exit;
>     prime := true;
>   end;
> 
> procedure init;
>   var i, j   : longint;
>       st  : string;
>   begin
>     randomize;
>     i := 0;
>     st := '';
>     while not seekeof(input) do
>       begin
>       inc( i );
>       readln( st );
>       while st[ length(st) ] = ' ' do
>         delete( st, length(st), 1 );
>       j := pos( ' ', st );
>       a[i].st1 := copy( st, 1, j-1 );
>       
>       delete( st, 1, j );
>       a[i].st2 := st;
>       
>       while length(a[i].st1)<9 do
>         a[i].st1 := ' ' + a[i].st1 ;
>         
>       while length(a[i].st2)<9 do
>         a[i].st2 := ' ' + a[i].st2 ;
>       end;
> 
>     n := i;
>     m := round( 2*n*1.2 );
>     while not prime(m) do
>       inc( m );
> 
>     for i := 1 to n do
>       begin
>       assign_code( a[i].st1, a[i].n1 );
>       assign_code( a[i].st2, a[i].n2 );
>       {writeln( a[i].n1,' ', a[i].n2 );}
>       end;
>     {close( input ); close( output ); halt}
>   end;
>       
> procedure work;
>   var i, j, p, q, pa, count    : longint;
>   begin
>     fillchar( d, sizeof(d), 0 );
>     fillchar( fa, sizeof(fa), 0 );
>     fillchar( rank, sizeof(rank), 0 );
>     for i := 1 to now do
>       fa[i] := i;
> 
>     for i := 1 to n do
>       begin
>       p := findpa( a[i].n1 );
>       q := findpa( a[i].n2 );
>       union( p, q );
>       inc( d[ a[i].n1 ] ); inc( d[ a[i].n2 ] );
>       end;
> 
>     count := 0;
>     pa := findpa( 1 );
>     if odd( d[1] ) then inc( count );
>     for i := 2 to now do
>       begin
>       j := findpa( i );
>       if j<>pa
>         then begin
>              writeln( 'Impossible' ); exit;
>              end;
>       if odd( d[i] ) then inc( count );
>       end;
>     if (count<>2)and(count<>0)
>       then begin
>            writeln( 'Impossible' ); exit;
>            end;
>     writeln( 'Possible' );
>   end;
>   
> begin
>   {assign( input,'2513.in' );
>   reset( input );
>   assign( output,'2513.out' );
>   rewrite( output );}
>   
>   init;
>   work;
>   
>   {close( input );
>   close( output );}
> 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