• <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 1141 - Closest Common Ancestors

            Posted on 2008-04-22 22:40 superman 閱讀(348) 評論(0)  編輯 收藏 引用 所屬分類: ZOJ
              1 /* Accepted 1141 C++ 00:03.20 5904K */
              2 #include <iostream>
              3 
              4 using namespace std;
              5 const int maxn = 800;
              6 
              7 struct { int cnt, son[maxn]; } Tree[maxn + 1];
              8 struct { int cnt, x[maxn]; } Query[maxn + 1];
              9 
             10 class DisjointSet
             11 {
             12 private:
             13      int p[maxn + 1], rank[maxn + 1];
             14 public:
             15      void reset()
             16      {
             17           memset(p, 0sizeof(p));
             18           memset(rank, 0sizeof(rank));
             19      }
             20      void make(const int x)
             21      {
             22           p[x] = x;
             23           rank[x] = 0;
             24      }
             25      void link(const int x, const int y)
             26      {
             27           if(rank[x] > rank[y])
             28                p[y] = x;
             29           else
             30           {
             31                p[x] = y;
             32                if(rank[x] == rank[y])
             33                     rank[y]++;
             34           }
             35      }
             36      int find(const int x)
             37      {
             38           if(x != p[x])
             39                p[x] = find(p[x]);
             40           return p[x];
             41      }
             42 }set;
             43 
             44 int ancestor[maxn + 1], cnt[maxn + 1];
             45 bool black[maxn + 1];
             46 
             47 void LCA(int u)
             48 {
             49      set.make(u);
             50      ancestor[set.find(u)] = u;
             51      for(int i = 0; i < Tree[u].cnt; i++)
             52      {
             53           LCA(Tree[u].son[i]);
             54           set.link(u, Tree[u].son[i]);
             55           ancestor[set.find(u)] = u;
             56      }
             57      black[u] = true;
             58      for(int i = 0; i < Query[u].cnt; i++)
             59           if(black[Query[u].x[i]])
             60                cnt[ancestor[set.find(Query[u].x[i])]]++;
             61 }
             62 
             63 int main()
             64 {
             65      int n;
             66      while(cin >> n)
             67      {
             68           memset(cnt,   0,     sizeof(cnt));
             69           memset(Tree,  0,     sizeof(Tree));
             70           memset(Query, 0,     sizeof(Query));
             71           memset(black, falsesizeof(black));
             72           
             73           int s, t, m;
             74           bool x[maxn] = {false};
             75           for(int i = 0; i < n; i++)
             76           {
             77                scanf("%d:(%d)"&s, &m);
             78                for(int k = 0; k < m; k++)
             79                {
             80                     cin >> t;
             81                     x[t] = true;
             82                     Tree[s].son[k] = t;
             83                }
             84                Tree[s].cnt = m;
             85           }
             86           
             87           int root;
             88           for(int i = 1; i <= n; i++)
             89                if(x[i] == false)
             90                {
             91                     root = i;
             92                     break;
             93                }
             94           
             95           cin >> m;
             96           char c1, c2, c3;
             97           for(int i = 0; i < m; i++)
             98           {
             99                cin >> c1 >> s >> c2 >> t >> c3;
            100                Query[s].x[Query[s].cnt++= t;
            101                Query[t].x[Query[t].cnt++= s;
            102           }
            103           
            104           set.reset();
            105           LCA(root);
            106           
            107           for(int i = 1; i <= n; i++)
            108                if(cnt[i])
            109                     cout << i << ':' << cnt[i] << endl;
            110      }
            111      
            112      return 0;
            113 }
            114 
            武侠古典久久婷婷狼人伊人| 国产免费久久精品99久久| 麻豆国内精品久久久久久| 久久精品二区| 2021最新久久久视精品爱| 亚洲AV日韩AV天堂久久| 国产亚洲精品久久久久秋霞| 97久久久精品综合88久久| 青青国产成人久久91网| 国产精品久久久久久久久软件| 国产香蕉久久精品综合网| 久久91精品国产91久久麻豆| 久久e热在这里只有国产中文精品99 | 狠狠色丁香婷婷久久综合五月| 精品国产日韩久久亚洲| 无码人妻久久一区二区三区| 91久久精品国产成人久久| 欧美日韩精品久久久久| 久久人人爽人人爽人人片AV不| 久久精品国产半推半就| 精品久久久久久国产| 99久久99久久精品国产| 亚洲国产精品无码久久SM| 久久99精品免费一区二区| 999久久久国产精品| 国产精品美女久久久久| 欧美精品一区二区久久| 国产精品九九久久免费视频| 国产亚洲精品美女久久久| 久久天天躁狠狠躁夜夜2020一 | 久久人人爽人人澡人人高潮AV | 91秦先生久久久久久久| 精品乱码久久久久久久| 久久久一本精品99久久精品66 | 亚洲国产天堂久久综合网站| 精品免费久久久久久久| 成人国内精品久久久久一区| 综合网日日天干夜夜久久| 久久www免费人成看片| 97精品伊人久久久大香线蕉| 久久久久久精品免费免费自慰|