拓撲序列 之 hdu 5154 Harry and Magical Computer

來源:互聯網
上載者:User

標籤:

/*AOV網(Activity On Vertex Network):用圖來表示工程:其中,用頂點表示活動;弧表示活動之間的制約關係。 工程是否順利進行?---->AOV網是否存在有向迴路 *******************************************用產生(包含所有) 頂點序列的方法,頂點序列滿足:在圖中,若頂點vi到頂點vj存在路徑,則在序列中,頂點vi領先於頂點vj。滿足上述條件的頂點序列稱為拓撲序列,產生這一序列的過程稱為拓撲排序。******************************************* AOV網是否含有有向迴路 <----> 是否可以產生拓撲序列  注意:此題測試資料中,對於重邊,儲存一條即可;對於自環也需要輸出"NO"。 */
  1 #include <iostream>  2 #include <cstdlib>  3 #include <cstdio>  4 #include <cstddef>  5 #include <iterator>  6 #include <algorithm>  7 #include <string>  8 #include <locale>  9 #include <cmath> 10 #include <vector> 11 #include <cstring> 12 #include <map> 13 #include <utility> 14 #include <queue> 15 #include <stack> 16 #include <set> 17 #include <functional> 18 using namespace std; 19 typedef pair<int, int> PII;  20 typedef long long int64; 21 const int INF = 0x3f3f3f3f; 22 const int modPrime = 3046721; 23 const double eps = 1e-9; 24 const int MaxN = 110; 25 const int MaxM = 10010; 26  27 int n, m; 28 bool G[MaxN][MaxN]; 29 int ndCnt[MaxN]; 30  31  32 void Solve() 33 { 34     int cnt = 0; 35     stack<int> stk; 36     for (int i = 0; i < n; ++i) 37     { 38         if (0 == ndCnt[i]) 39         { 40             stk.push(i); 41         } 42     } 43     while (!stk.empty()) 44     { 45         int node = stk.top(); 46         stk.pop(); 47         ++cnt; 48         for (int i = 0; i < n; ++i) 49         { 50             if (G[node][i]) 51             { 52                 --ndCnt[i]; 53                 if (0 == ndCnt[i]) 54                 { 55                     stk.push(i); 56                 } 57             } 58         } 59     } 60     if (cnt == n) 61     { 62         printf("YES\n"); 63     } 64     else 65     { 66         printf("NO\n"); 67     } 68 } 69  70 int main() 71 { 72 #ifdef HOME 73     freopen("in", "r", stdin); 74     //freopen("out", "w", stdout); 75 #endif 76     while (~scanf("%d %d", &n, &m)) 77     { 78         for (int i = 0; i < n; ++i) 79         { 80             ndCnt[i] = 0; 81             for (int j = 0; j < n; ++j) 82             { 83                 G[i][j] = false; 84             } 85         } 86  87         int a, b; 88         for (int i = 0; i < m; ++i) 89         { 90             scanf("%d %d", &a, &b); 91             // 對於重邊的處理辦法 92             if (!G[b - 1][a - 1]) 93             { 94                 G[b - 1][a - 1] = true; 95                 ++ndCnt[a - 1]; 96             } 97         } 98         Solve(); 99     }100 101 #ifdef HOME102     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;103     _CrtDumpMemoryLeaks();104 #endif105     return 0;106 }

 

 

拓撲序列 之 hdu 5154 Harry and Magical Computer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.