Topological sort of a small sum

#include <the iostream> 
#include <cstdio> 
#include <CString>
 the using  namespace STD;
 const  int MAXN = 100 + 15 ;
 int n, m, T; // figure, there are n nodes 
int TOPO [MAXN]; // topological sort order stored 
int the staus [MAXN]; // represents transient 
BOOL Grape [MAXN] [MAXN]; // represents dependencies 
BOOL DFS ( int U) 
{ 
    the staus [U] = - . 1 ; // represents progress deep search 
    for ( int v =. 1 ; V <= n-; ++ V) 
    { 
        IF (Grape [U] [V]) 
        { 
            IF (the staus [V] < 0 )
                 return  to false ; // specify the presence of ring 
            IF (the staus [V] DFS &&!! (V)) // if not been accessed 
                return  to false ; 
        } 
    } 
    the staus [U] = . 1 ; // represents search has completed and returns to the deep state 
    TOPO [- T] = U;
     return  to true ; 
} 
BOOL topoSort ( ) 
{ 
    T = n-; 
    Memset (the staus, 0, The sizeof (the staus)); // node have not been accessed 
    for ( int U = . 1 ; U <= n-; U ++) // deep search every node 
    {
         IF ! (The staus [U]) // when unvisited 
            IF (! DFS (U))
                 return  to false ; 
    } 
    return  to true ; 
} 
int main () 
{ 
    the while (CIN >> >> n- m) 
    { 
        Memset (Grape, to false , the sizeof (Grape));
         IF (n- == 0 && m == 0)
            break;  
        int u,v;
        for(int i=0;i!=m;++i)
        {
            cin>>u>>v;
            Grape[u][v] = true;
        }
        topoSort();
        cout<<topo[0];
        for(int i=1;i!=n;++i)
            cout<<" "<<topo[i];
        cout<<endl;
    }
}
View Code

UVA 10305

_______________________________________2019 914 unfinished

Guess you like

Origin www.cnblogs.com/newstartCY/p/11519496.html