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

极其伪的代码

Posted by jinzhihan at 2013-10-28 23:21:16 on Problem 2553
var
  a,b,aa,bb:array[0..50001] of longint;
  c,low,visit,dfn,size,f,g,h,l,ll,r,rr:array[0..5001] of longint;
  n,m,nn,mm,ans,ss,num,i,j,k,time:longint;

procedure sort(l,r:longint);
var
  i,j,x,y:longint;
begin
  i:=l; j:=r; x:=a[(l+r) shr 1];
  repeat
    while a[i]<x do inc(i);
    while x<a[j] do dec(j);
    if not(i>j)
      then
        begin
          y:=a[i]; a[i]:=a[j]; a[j]:=y;
          y:=b[i]; b[i]:=b[j]; b[j]:=y;
          inc(i); dec(j);
        end;
  until i>j;
  if l<j then sort(l,j);
  if i<r then sort(i,r);
end;

procedure tarjan(k:longint);
var
  i:longint;
begin
  inc(time); low[k]:=time; dfn[k]:=time; visit[k]:=1;
  inc(num); c[num]:=k;
  for i:=l[k] to r[k] do
    if visit[b[i]]=0
      then
        begin
          tarjan(b[i]);
          if low[b[i]]<low[k]
            then low[k]:=low[b[i]];
        end
      else
        if visit[b[i]]=1
          then
            begin
              if dfn[b[i]]<low[k]
                then low[k]:=dfn[b[i]];
            end;
  if low[k]=dfn[k]
    then
      begin
        inc(nn);
        size[nn]:=1;
        while low[c[num]]<>dfn[c[num]] do
          begin
            f[c[num]]:=nn;
            visit[c[num]]:=2;
            inc(ss);
            g[ss]:=c[num]; h[ss]:=nn;
            inc(size[nn]);
            dec(num);
          end;
        f[k]:=nn;
        visit[k]:=2;
        inc(ss);
        g[ss]:=k; h[ss]:=nn;
        dec(num);
      end;
end;

begin
  while true do
    begin
      read(n);
      if n=0 then halt;
      fillchar(a,sizeof(a),0);
      fillchar(b,sizeof(b),0);
      fillchar(aa,sizeof(aa),0);
      fillchar(bb,sizeof(bb),0);
      fillchar(c,sizeof(c),0);
      fillchar(low,sizeof(low),0);
      fillchar(visit,sizeof(visit),0);
      fillchar(dfn,sizeof(dfn),0);
      fillchar(size,sizeof(size),0);
      fillchar(f,sizeof(f),0);
      fillchar(g,sizeof(g),0);
      fillchar(h,sizeof(h),0);
      fillchar(l,sizeof(l),0);
      fillchar(ll,sizeof(ll),0);
      fillchar(r,sizeof(r),0);
      fillchar(rr,sizeof(rr),0);
      read(m);
      for i:=1 to m do
        read(a[i],b[i]);            

      sort(1,m);
      l[a[1]]:=1;
      for i:=2 to m do
        if a[i]<>a[i-1]
          then
            begin
              r[a[i-1]]:=i-1;
              l[a[i]]:=i;
            end;
      r[a[m]]:=m;
      for i:=1 to n do
        if r[i]=0 then r[i]:=-1;                        

      for i:=1 to n do visit[i]:=0;
      time:=0; nn:=0; num:=0; ss:=0;
      for i:=1 to n do
        if visit[i]=0 then tarjan(i);
      mm:=0; j:=1;
      for i:=1 to nn do
        if h[j]=i
          then
            begin
              for k:=1 to nn do visit[k]:=0;
              visit[i]:=1;
              while (j<=n) and (h[j]=i) do
                begin
                  for k:=l[g[j]] to r[g[j]] do
                    if visit[f[b[k]]]=0
                      then
                        begin
                          visit[f[b[k]]]:=1;
                          inc(mm);
                          aa[mm]:=i;
                          bb[mm]:=f[b[k]];
                        end;
                  inc(j);
                end;
            end;
      for i:=1 to nn do begin ll[i]:=0; rr[i]:=0; end;
      ll[aa[1]]:=1;
      for i:=2 to mm do
        if aa[i]<>aa[i-1]
          then
            begin
              rr[aa[i-1]]:=i-1;
              ll[aa[i]]:=i;
            end;
      rr[aa[mm]]:=mm;                                    

      ss:=0;
      for i:=1 to n do
        if rr[f[i]]=0
          then
            begin
              if ss=1 then write(' ');
              ss:=1;
              write(i);
            end;
      writeln;       
    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