• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            superman

            聚精會神搞建設(shè) 一心一意謀發(fā)展
            posts - 190, comments - 17, trackbacks - 0, articles - 0
               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            ZOJ 1060 - Sorting It All Out

            Posted on 2008-04-03 18:16 superman 閱讀(339) 評論(0)  編輯 收藏 引用 所屬分類: ZOJ
              1 /* Accepted 1060 C++ 00:00.06 848K */
              2 #include <string>
              3 #include <iostream>
              4 
              5 using namespace std;
              6 
              7 class Graph
              8 {
              9 private:
             10     int n, e;
             11     int count[26];
             12     bool map[26][26];
             13 public:
             14     Graph(const int nn)
             15     {
             16         n = nn; e = 0;
             17         memset(count, 0sizeof(count));
             18         memset(map, falsesizeof(map));
             19     }
             20     void AddEdge(const int s, const int t)
             21     {
             22         e++;
             23         if(map[s][t] == false)
             24             count[t]++;
             25         map[s][t] = true;
             26     }
             27     bool TopSort(int x[])
             28     {
             29         int cn = n;
             30         int cnt[26];
             31         memcpy(cnt, count, sizeof(count));
             32         
             33         while(cn--)
             34         {
             35             int tot = 0, index;
             36             for(int i = 0; i < n; i++)
             37                 if(cnt[i] == 0)
             38                 {
             39                     index = i;
             40                     tot++;
             41                 }
             42             if(tot > 1)
             43                 return 0;
             44             if(tot == 1)
             45             {
             46                 for(int i = 0; i < n; i++)
             47                     if(map[index][i])
             48                         cnt[i]--;
             49                 x[cn] = index;
             50                 cnt[index] = -1;
             51             }
             52         }
             53         return 1;
             54     }
             55     bool ExistCycle()
             56     {
             57         bool f[26][26];
             58         memcpy(f, map, sizeof(map));
             59         for(int k = 0; k < n; k++)
             60         for(int i = 0; i < n; i++)
             61         for(int j = 0; j < n; j++)
             62             if(f[i][k] && f[k][j])
             63                 f[i][j] = 1;
             64         for(int i = 0; i < n; i++)
             65             if(f[i][i])
             66                 return 1;
             67         return 0;
             68     }
             69 };
             70 
             71 int main()
             72 {
             73     int n, m;
             74     string relation;
             75     while((cin >> n >> m) && n && m)
             76     {
             77         Graph G(n);
             78         bool find = false;
             79         for(int i = 1; i <= m; i++)
             80         {
             81             cin >> relation;
             82             
             83             if(find)
             84                 continue;
             85             
             86             G.AddEdge(relation[0- 'A', relation[2- 'A');
             87             
             88             if(G.ExistCycle())
             89             {
             90                 cout << "Inconsistency found after " << i << " relations." << endl;
             91                 find = true;
             92                 continue;
             93             }
             94             
             95             int x[26];
             96             if(G.TopSort(x))
             97             {
             98                 cout << "Sorted sequence determined after " << i << " relations: ";
             99                 for(int i = n - 1; i >= 0; i--)
            100                     cout << char(x[i] + 'A');
            101                 cout << '.' << endl;
            102                 find = true;
            103             }
            104         }
            105         if(find == false)
            106             cout << "Sorted sequence cannot be determined." << endl;
            107     }
            108     
            109     return 0;
            110 }
            111 
            久久93精品国产91久久综合| 日韩美女18网站久久精品| 伊人久久综合无码成人网| 一本久久a久久精品vr综合| 久久久精品人妻一区二区三区四| 精品久久久噜噜噜久久久| 91麻豆精品国产91久久久久久| 久久w5ww成w人免费| 亚洲国产精品久久久久网站| 国产精品久久久久免费a∨| 久久久久一区二区三区| 亚洲国产成人乱码精品女人久久久不卡| 漂亮人妻被中出中文字幕久久| www.久久热.com| 精品一二三区久久aaa片| 99久久伊人精品综合观看| 午夜欧美精品久久久久久久| 亚洲国产成人久久精品动漫| 无码日韩人妻精品久久蜜桃| 久久人人爽人爽人人爽av| 99久久超碰中文字幕伊人| 久久久久久国产精品美女| 国产精品美女久久久免费| 久久国产精品一国产精品金尊| 亚洲人成无码久久电影网站| 91久久精品视频| 中文精品久久久久国产网址| 亚洲伊人久久精品影院| 国产成人综合久久精品红 | 色综合久久久久综合99| 久久美女网站免费| 久久国产精品77777| 性高湖久久久久久久久| 综合久久给合久久狠狠狠97色| 久久国产精品一区| 2021国产成人精品久久| 欧美精品一本久久男人的天堂| 国产成人精品免费久久久久| 国产婷婷成人久久Av免费高清| 久久精品欧美日韩精品| 国产成人久久精品一区二区三区|