日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj2367 Genealogical tree

發(fā)布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj2367 Genealogical tree 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

思路:

拓?fù)渑判?#xff0c;這里是用染色的dfs實現(xiàn)的。在有環(huán)的情況下可以判斷出來,沒有環(huán)的情況下輸出拓?fù)渑判蛐蛄小?/p>

實現(xiàn):

1 #include <vector> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #define w 0 6 #define g 1 7 #define b 2 8 using namespace std; 9 const int N = 105; 10 int G[N][N]; 11 vector<int> res, vis; 12 bool cycle; 13 int n, m; 14 void init() 15 { 16 cycle = false; 17 cin >> n; 18 memset(G, 0, sizeof G); 19 for (int i = 0; i <= n; i++) vis.push_back(w); 20 int x; 21 for (int i = 1; i <= n; i++) 22 { 23 while (cin >> x, x) 24 { 25 G[i][x] = 1; 26 } 27 } 28 } 29 void dfs(int u) 30 { 31 if (vis[u] == b) return; 32 if (vis[u] == g) { cycle = true; return; } 33 vis[u] = g; 34 for (int i = n; i >= 1; i--) 35 { 36 if (G[u][i]) dfs(i); 37 } 38 vis[u] = b; 39 res.push_back(u); 40 return; 41 } 42 void topsort() 43 { 44 for (int i = n; i >= 1; i--) 45 { 46 if (vis[i] == w) dfs(i); 47 } 48 reverse(res.begin(), res.end()); 49 } 50 int main() 51 { 52 init(); 53 topsort(); 54 if (cycle) cout << "There is a cycle in G!" << endl; 55 else 56 { 57 for (int i = 0; i < res.size(); i++) 58 cout << res[i] << " "; 59 cout << endl; 60 } 61 return 0; 62 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/wangyiming/p/7617959.html

總結(jié)

以上是生活随笔為你收集整理的poj2367 Genealogical tree的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。