• <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

            聚精會神搞建設 一心一意謀發展
            posts - 190, comments - 17, trackbacks - 0, articles - 0
               :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            ZOJ 1060 - Sorting It All Out

            Posted on 2008-04-03 18:16 superman 閱讀(338) 評論(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 
            欧美日韩中文字幕久久伊人| 精品久久综合1区2区3区激情| 国产成人久久久精品二区三区| 亚洲国产精品久久久久婷婷软件| 中文字幕久久欲求不满| 久久亚洲视频| 精品久久久久久中文字幕大豆网| 新狼窝色AV性久久久久久| 久久久久四虎国产精品| 中文字幕精品久久久久人妻| 久久久久亚洲AV成人片| 久久久久国产成人精品亚洲午夜| 色综合久久无码中文字幕| 99久久免费只有精品国产| 久久99国产乱子伦精品免费| AV色综合久久天堂AV色综合在| 99久久99久久精品国产片| 天天躁日日躁狠狠久久| 久久久久人妻精品一区三寸蜜桃| 蜜臀久久99精品久久久久久小说| 久久久精品久久久久特色影视| 久久国产免费观看精品3| 亚洲国产精品成人久久蜜臀 | 国产精品99久久久久久www| 欧美精品一区二区久久| 91精品国产91久久| 激情伊人五月天久久综合| 热RE99久久精品国产66热| 国产99久久久久久免费看| 国产午夜精品理论片久久影视| 性欧美丰满熟妇XXXX性久久久| 亚洲а∨天堂久久精品9966| 91超碰碰碰碰久久久久久综合| 久久青青草原亚洲av无码app| 亚洲天堂久久久| 欧美亚洲国产精品久久高清| 日本加勒比久久精品| 无码人妻少妇久久中文字幕| 久久中文字幕视频、最近更新| 久久久精品国产亚洲成人满18免费网站 | 久久久久99精品成人片直播|