poj 2762 tarjan缩点+拓扑序

系统 1611 0

2013-09-08 10:00

      
        var
      
      
        

    m, n                :longint;

    t                   :longint;

    f, last             :
      
      
        array
      
      [
      
        0
      
      ..
      
        20100
      
      ] 
      
        of
      
      
         longint;

    pre, other          :
      
      
        array
      
      [
      
        0
      
      ..
      
        160100
      
      ] 
      
        of
      
      
         longint;

    l, time             :longint;

    dfn, low            :
      
      
        array
      
      [
      
        0
      
      ..
      
        20100
      
      ] 
      
        of
      
      
         longint;

    tot                 :longint;

    stack               :
      
      
        array
      
      [
      
        0
      
      ..
      
        20100
      
      ] 
      
        of
      
      
         longint;

    flag, fs            :
      
      
        array
      
      [
      
        0
      
      ..
      
        20100
      
      ] 
      
        of
      
      
         boolean;

    i                   :longint;

    key                 :
      
      
        array
      
      [
      
        0
      
      ..
      
        20100
      
      ] 
      
        of
      
      
         longint;

    kk                  :longint;

    que                 :
      
      
        array
      
      [
      
        0
      
      ..
      
        20100
      
      ] 
      
        of
      
      
         longint;

    count               :longint;

 


      
      
        function
      
      
         min(a,b:longint):longint;


      
      
        begin
      
      
        if
      
       a>b 
      
        then
      
       min:=b 
      
        else
      
       min:=
      
        a;


      
      
        end
      
      
        ;

 


      
      
        procedure
      
      
         connect(x,y:longint);


      
      
        begin
      
      
        

    inc(l);

    pre[l]:
      
      =
      
        last[x];

    last[x]:
      
      =
      
        l;

    other[l]:
      
      =
      
        y;

    f[y]:
      
      =
      
        x;


      
      
        end
      
      
        ;

 


      
      
        procedure
      
      
         init;


      
      
        var
      
      
        

    i                   :longint;

    x, y                :longint;


      
      
        begin
      
      
        

    read(n,m);

    
      
      
        for
      
       i:=
      
        1
      
      
        to
      
       m 
      
        do
      
      
        begin
      
      
        

        read(x,y);

        connect(x,y);

    
      
      
        end
      
      
        ;


      
      
        end
      
      
        ;

 


      
      
        procedure
      
      
         dfs(x:longint);


      
      
        var
      
      
        

    p, q, cur           :longint;


      
      
        begin
      
      
        

    inc(time);

    dfn[x]:
      
      =
      
        time;

    low[x]:
      
      =
      
        time;

    inc(tot);

    stack[tot]:
      
      =
      
        x;

    fs[x]:
      
      =
      
        true;

    flag[x]:
      
      =
      
        true;

    q:
      
      =
      
        last[x];

    
      
      
        while
      
       q<>
      
        0
      
      
        do
      
      
        begin
      
      
        

        p:
      
      =
      
        other[q];

        
      
      
        if
      
       p<>x 
      
        then
      
      
        begin
      
      
        if
      
      
        not
      
       flag[p] 
      
        then
      
      
        begin
      
      
        

                dfs(p);

                low[x]:
      
      =
      
        min(low[x],low[p]);

            
      
      
        end
      
      
        else
      
      
        if
      
       fs[p] 
      
        then
      
      
        begin
      
      
        

                low[x]:
      
      =
      
        min(low[x],dfn[p]);

            
      
      
        end
      
      
        ;

        
      
      
        end
      
      
        ;

        q:
      
      =
      
        pre[q];

    
      
      
        end
      
      
        ;

    p:
      
      =-
      
        1
      
      
        ;

    
      
      
        if
      
       low[x]=dfn[x] 
      
        then
      
      
        begin
      
      
        

        inc(kk);

        
      
      
        while
      
       p<>x 
      
        do
      
      
        begin
      
      
        

            p:
      
      =
      
        stack[tot];

            fs[p]:
      
      =
      
        false;

            key[p]:
      
      =
      
        kk;

            dec(tot);

            inc(count);

        
      
      
        end
      
      
        ;

    
      
      
        end
      
      
        ;

 


      
      
        end
      
      
        ;

 


      
      
        function
      
      
         bfs(x:longint):boolean;


      
      
        var
      
      
        

    i                   :longint;

    t, h, p, q          :longint;

    cur                 :longint;

    d                   :
      
      
        array
      
      [
      
        0
      
      ..
      
        2020
      
      ] 
      
        of
      
      
         longint;

 


      
      
        begin
      
      
        

        fillchar(flag,sizeof(flag),
      
      
        0
      
      
        );

        fillchar(d,sizeof(d),
      
      
        0
      
      
        );

        h:
      
      =
      
        0
      
      ; t:=
      
        1
      
      
        ;

        que[
      
      
        1
      
      ]:=
      
        x;

        d[x]:
      
      =
      
        1
      
      
        ;

        
      
      
        while
      
       h<t 
      
        do
      
      
        begin
      
      
        

            inc(h);

            cur:
      
      =
      
        que[h];

            q:
      
      =
      
        last[cur];

            
      
      
        while
      
       q<>
      
        0
      
      
        do
      
      
        begin
      
      
        

                p:
      
      =
      
        other[q];

                inc(t);

                que[t]:
      
      =
      
        p;

                d[p]:
      
      =d[cur]+
      
        1
      
      
        ;

                q:
      
      =
      
        pre[q];

            
      
      
        end
      
      
        ;

        
      
      
        end
      
      
        ;

        
      
      
        if
      
       d[que[t]]=kk-n 
      
        then
      
       exit(true) 
      
        else
      
      
         exit(false);


      
      
        end
      
      
        ;

 


      
      
        procedure
      
      
         main;


      
      
        var
      
      
        

    i                   :longint;

    x                   :longint;

    q, p                :longint;


      
      
        begin
      
      
        

    l:
      
      =
      
        1
      
      
        ;

    fillchar(last,sizeof(last),
      
      
        0
      
      
        );

    time:
      
      =
      
        0
      
      
        ;

    fillchar(f,sizeof(f),
      
      
        0
      
      
        );

    fillchar(low,sizeof(low),
      
      
        0
      
      
        );

    fillchar(dfn,sizeof(dfn),
      
      
        0
      
      
        );

    fillchar(flag,sizeof(flag),false);

    fillchar(stack,sizeof(stack),
      
      
        0
      
      
        );

    tot:
      
      =
      
        0
      
      
        ;

    fillchar(fs,sizeof(fs),false);

    fillchar(key,sizeof(key),
      
      
        0
      
      
        );

    count:
      
      =
      
        0
      
      
        ;

    init;

    x:
      
      =
      
        0
      
      
        ;

    kk:
      
      =
      
        n;

    
      
      
        for
      
       i:=
      
        1
      
      
        to
      
       n 
      
        do
      
      
        if
      
       (f[i]=
      
        0
      
      ) 
      
        then
      
      
        begin
      
      
        if
      
       x<>
      
        0
      
      
        then
      
      
        begin
      
      
        

                writeln(
      
      
        '
      
      
        No
      
      
        '
      
      
        );

                exit;

            
      
      
        end
      
      
        ;

            x:
      
      =
      
        i;

        
      
      
        end
      
      
        ;

    
      
      
        if
      
       x=
      
        0
      
      
        then
      
       x:=
      
        1
      
      
        ;

    dfs(x);

 

    
      
      
        if
      
       count<>n 
      
        then
      
      
        begin
      
      
        

        writeln(
      
      
        '
      
      
        No
      
      
        '
      
      
        );

        exit;

    
      
      
        end
      
      
        ;

 

    
      
      
        for
      
       i:=
      
        1
      
      
        to
      
       n 
      
        do
      
      
        begin
      
      
        

        q:
      
      =
      
        last[i];

        
      
      
        while
      
       q<>
      
        0
      
      
        do
      
      
        begin
      
      
        

            p:
      
      =
      
        other[q];

            
      
      
        if
      
       key[i]<>key[p] 
      
        then
      
      
         connect(key[i],key[p]);

            q:
      
      =
      
        pre[q];

        
      
      
        end
      
      
        ;

    
      
      
        end
      
      
        ;

    x:
      
      =
      
        0
      
      
        ;

    
      
      
        for
      
       i:=n+
      
        1
      
      
        to
      
       kk 
      
        do
      
      
        begin
      
      
        if
      
       f[i]=
      
        0
      
      
        then
      
      
        begin
      
      
        if
      
       x<>
      
        0
      
      
        then
      
      
        begin
      
      
        

                writeln(
      
      
        '
      
      
        No
      
      
        '
      
      
        );

                exit;

            
      
      
        end
      
      
        ;

            x:
      
      =
      
        i;

        
      
      
        end
      
      
        ;

    
      
      
        end
      
      
        ;

 

    
      
      
        if
      
       x=
      
        0
      
      
        then
      
       x:=
      
        1
      
      
        ;

    
      
      
        if
      
       bfs(x) 
      
        then
      
       writeln(
      
        '
      
      
        Yes
      
      
        '
      
      ) 
      
        else
      
       writeln(
      
        '
      
      
        No
      
      
        '
      
      
        );


      
      
        end
      
      
        ;

 


      
      
        begin
      
      
        

    read(t);

    
      
      
        for
      
       i:=
      
        1
      
      
        to
      
       t 
      
        do
      
      
         main;


      
      
        end
      
      .
    

 

poj 2762 tarjan缩点+拓扑序


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论