uva 297(傳遞閉包 WF 1996)

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   http   

題意:在一張有向圖中輸出所有的環。

思路:先用Floyd求傳遞閉包,然後通過傳遞閉包建圖若是Map[i][j] && Map[j][i]則建一條無向邊。然後圖中所有的連通分支即為一個環。

代碼如下:

  1 /**************************************************  2  * Author     : xiaohao Z  3  * Blog     : http://www.cnblogs.com/shu-xiaohao/  4  * Last modified : 2014-06-26 11:12  5  * Filename     : uva_247.cpp  6  * Description     :   7  * ************************************************/  8   9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22  23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30  31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 1010; 34 vector<int> Map[LEN], ans[LEN]; 35 map<string, int> mp; 36 string a, b, name[LEN]; 37 int n, m, top, cnt, Mp[LEN][LEN], vis[LEN]; 38  39 int ch(string a){ 40     if(!mp.count(a)) { 41         name[top] = a; 42         mp[a] = top++; 43     } 44     return mp[a]; 45 } 46  47 void Floyd(){ 48     for(int k=0; k<n; k++){ 49         for(int i=0; i<n; i++){ 50             for(int j=0; j<n; j++){ 51                 if(Mp[i][j] || (Mp[i][k] && Mp[k][j])) { 52                     Mp[i][j] = 1; 53                 } 54             } 55         } 56     } 57 } 58  59 void dfs(int v, int tag){ 60     vis[v] = tag; 61     for(int i=0; i<Map[v].size(); i++){ 62         int x = Map[v][i]; 63         if(vis[x] < 0) dfs(x, tag); 64     } 65 } 66  67 void solve(){ 68     for(int i=0; i<n; i++){ 69         for(int j=0; j<n; j++){ 70             if(Mp[i][j] && Mp[j][i]){ 71                 Map[i].PB(j); 72                 Map[j].PB(i); 73             } 74         } 75     } 76     cnt = 0; 77     memset(vis, -1, sizeof vis); 78     for(int i=0; i<n; i++){ 79         if(vis[i] < 0) dfs(i, cnt++); 80     } 81     for(int i=0; i<cnt; i++){ 82         for(int j=0; j<n; j++){ 83             if(vis[j] == i){ 84                 ans[i].PB(j); 85             } 86         } 87     } 88 } 89  90 void out(int &kase){ 91     if(kase != 1) cout << endl; 92     cout << "Calling circles for data set "<< kase++ << ":" << endl; 93     for(int i=0; i<cnt; i++){ 94         for(int j=0; j<ans[i].size(); j++){ 95             cout << name[ans[i][j]]; 96             if(j != ans[i].size()-1)cout << ", "; 97         } 98         cout << endl; 99     }100 }101 102 int main()103 {104 //    freopen("in.txt", "r", stdin);105 106     ios::sync_with_stdio(false);107     int kase = 1;108     while(cin >> n >> m){109         if(!n && !m) break;110         memset(Mp, 0, sizeof Mp);111         for(int i=0; i<LEN; i++) Map[i].clear();112         for(int i=0; i<LEN; i++) ans[i].clear();113         mp.clear();top = 0;114         for(int i=0; i<m; i++){115             cin >> a >> b;116             int na = ch(a), nb = ch(b);117             Mp[na][nb] = 1;118         }119         Floyd();120         solve();121         out(kase);122     }123     return 0;124 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.