zoukankan      html  css  js  c++  java
  • Tarjan Pascal程序

    program tarjan;
    
    Var
     a:array[0..1000,0..1000] of boolean;
     stack,dfn,low:array[0..1000] of longint;
     v:array[0..1000] of boolean;
     top,all,i,p,q,n,m:longint;
    
    Procedure fopen;
      begin
      assign(input,'tarjan.in');
      assign(output,'tarjan.out');
      reset(input);
      rewrite(output);
    end;
    
    Procedure fclose;
      begin
      close(input);
      close(output);
    end;
    
    Function min(a,b:longint):longint;
      begin
      if a<b then exit(a) else exit(b);
    end;
    
    Procedure tarjan(P:longint);
    var
     i:longint;
      begin
      Writeln('Enter P=',p,' low[p]=dfn[p]=',all+1);
      inc(all);
      low[p]:=all;
      dfn[p]:=all;
      inc(top);
      stack[top]:=p;
      v[p]:=true;
      for i:=1 to n do
        if a[p,i] then
          begin
          if dfn[i]=0 then begin tarjan(i); low[p]:=min(low[p],low[i]);end else if v[i] then 
          low[p]:=min(low[p],low[i]);
        end;
      if low[p]=dfn[p] then
        repeat
        v[stack[top]]:=false;
        writeln(p,':',stack[top]);
        dec(top);
      until stack[top+1]=p;
      writeln('Exit P=',p,' low[p]=',low[p],' dfn[p]=',dfn[p]);
    end;
      
      begin
      fopen;
      readln(n,m);
      fillchar(a,sizeof(a),false);
      fillchar(v,sizeof(v),false);
      all:=0;top:=0;
      for i:=1 to m do
        begin
        readln(p,q);
        a[p,q]:=true;
      end;
      for i:=1 to n do
        if dfn[i]=0 then tarjan(i);
      fclose;
    end.
    
  • 相关阅读:
    leetcode 78. 子集 JAVA
    leetcode 91. 解码方法 JAVA
    leetcode 75. 颜色分类 JAVA
    leetcode 74 搜索二维矩阵 java
    leetcode 84. 柱状图中最大的矩形 JAVA
    last occurance
    first occurance
    classical binary search
    LC.234.Palindrome Linked List
    LC.142. Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/htfy/p/2764451.html
Copyright © 2011-2022 走看看