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 21002tyj at 2013-10-16 13:35:04 on Problem 3687
Program poj3687;
Var
  n,m,tot,t:longint;
  ru:array[0..201] of longint;
  pre,son:array[0..50002] of longint;
  now,ans:array[0..201] of longint;
  use:array[0..201] of boolean;

  Procedure build(a,b:longint);
  begin
    inc(tot);
    pre[tot]:=now[a];
    now[a]:=tot;
    son[tot]:=b;
  end;

  Procedure init;
  var
    i,j,a,b:longint;
  begin
    readln(n,m);
    fillchar(ru,sizeof(ru),0);
    fillchar(now,sizeof(now),0);
    fillchar(use,sizeof(use),false);
    for i:=1 to m do
      begin
        readln(a,b);
        inc(ru[a]);
        build(b,a);
      end;
  end;

  Procedure work;
  var
    i,j,p,sum:longint;
  begin
    sum:=0;
    for i:=n downto 1 do
      begin
        for j:=n downto 1 do
        if (ru[j]=0) and (not use[j]) then
          begin
            ans[j]:=i;
            inc(sum);
            use[j]:=true;
            p:=now[j];
            while p<>0 do
              begin
                dec(ru[son[p]]);
                p:=pre[p];
              end;
            break;
          end;
      end;
    if sum <> n then
      begin
        writeln(-1);
        exit;
      end;
    for i:=1 to n do
      write(ans[i],' ');
    writeln;
  end;

  Procedure print;
  var i:longint;
  begin
    readln(t);
    for i:=1 to t do
    begin
      init;
      work;
    end;
  end;

Begin
  print;
  readln;
  readln;
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