AcWing 848. topological sequence digraph

Analog queue

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m;
int h[N], e[N], ne[N], idx;//邻接表
int d[N];//入度
int q[N];//队列
void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
bool topsort() {
    int hh = 0, tt = -1;
     For ( int I = . 1 ; I <= n-; I ++ )
         IF (D [I]!) // first of all the point 0 is inserted into the queue 
            Q [TT ++] = I; // enqueued 
    the while (HH <= TT) {
         int T = Q [HH ++]; // remove schedule is 0 
        for ( int I = H [T];! = I - . 1 ; NE = I [I]) { // all points i and connected, and is connected to a point of the point 0
             // penetration must be reduced by one 
            int J = E [i];   // find the edge 
            D [J] -; / / so that the degree of reduction, i.e. the cut edge of the 
            IF (D [J] == 0) // when the progress is reduced to 0, into the queue 
                Q [TT ++] = J; 
        } 
    } 
    return TT n-== - . 1 ; // determine whether all push 
}
 int main () { 
    Scanf ( " % D% D " , & n-, & m); 
    Memset (H, - . 1 , the sizeof H);
     for ( int I = 0 ; I <m; I ++ ) {
         int A, B; 
        Scanf ( " % D% D " , & A , & B);  
        the Add (A, B);
        D [B] ++ ;
    }
    if (!topsort()) puts("-1");
    else {
        for (int i = 0; i < n; i ++ ) printf("%d ", q[i]);
        puts("");
    }
    return 0;
}

 

queue

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int  const N = 1E5 + 10 ;
 int E [N], NE [N], H [N], IDX, D [N];
 int n-, m; 
Vector < int > ANS;
 // build adjacency table 
void the Add ( int A, int B) { 
    E [IDX] = B, NE [IDX] = H [A], H [A] = IDX ++ ; 
} 

// topological sorting 
void top_sort () { 
    queue < int > Q;   // maintains a queue 
    for ( int I =. 1 ; I <= n-; I ++) IF (D [I]) q.push (I);!   // the 0-degree point enqueue
     // When the queue is not empty 
    the while (q.size ()) { 
        Auto T = q.front ();   // get HOL 
        q.pop ();   // HOL dequeued 
        ans.push_back (T);   // this number sequence into the answer 
        for ( int I H = [T];! = I - . 1 ; NE = I [I]) { // enumerate all the head elements adjacent element 
            int J = E [I]; 
            D [J] -;   // Force dequeue header element corresponding to the penetration of the head elements and the elements connected to a minus 
            IF (D [J]!) q.push (J);   // put into the element of 0 in the queue
        } 
    } 
    IF (ans.size () == n-) { // output answer sequence 
        for (A Auto: ANS) the printf ( " % D " , A); 
    } the else COUT << " -1 " ; 
} 

int main ( ) { 
    CIN >> >> n-m;   // input points and edges 
    Memset (H, - . 1 , the sizeof H);   // initialize H 
    for ( int I = 0 ; I <m; I ++) { // reading each edge 
        int A, B; 
        Scanf ( " % D% D" , & A, & b); 
        the Add (a, b);   // the b side is inserted a table 
        D [b] ++;   // penetration of a plus b 
    } 
    top_sort (); 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11831954.html