posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          并查集 (2) - PKU1611 The Suspects

          Posted on 2007-07-15 11:12 ZelluX 閱讀(481) 評(píng)論(0)  編輯  收藏 所屬分類: Algorithm
          突然想通了一開始一直超時(shí)的原因。
          每次我都是把新的suspect并入第一個(gè)元素所在的集合中,但是由于使用了優(yōu)化后的并集操作,有可能是第一個(gè)元素所在的集合并入了新的suspect所在的集合,也就是說(shuō)此后last變量并沒(méi)有指向第一個(gè)元素所在集合的跟結(jié)點(diǎn)。于是在Union方法中,parent[root1]可能得到一個(gè)正整數(shù),并導(dǎo)致Find方法死循環(huán)(根結(jié)點(diǎn)的parent為正)

          后來(lái)把Find方法放到Union方法中,問(wèn)題解決了。

          #include <iostream>

          using namespace std;

          const int DEFAULT_SIZE = 100;

          class UFSets
          {
          private:
              
          int *parent;
              
          int size;
          public:
              UFSets(
          int s = DEFAULT_SIZE);
              
          ~UFSets() { delete [] parent; }
              
          void Union(int root1, int root2);
              
          int Find(int x);
              
          void Clear(int n);
          };

          UFSets::UFSets(
          int s)
          {
              size 
          = s;
              parent 
          = new int[size + 1];
              
          for (int i = 0; i <= size; i++)
                  parent[i] 
          = -1;
          }

          int UFSets::Find(int x)
          {
              
          int result = x;
              
          while (parent[result] >= 0)
                  result 
          = parent[result];
              
          return result;
          }

          void UFSets::Union(int root1, int root2)
          {
              
          // The old version didn't contain the following 3 setences.
              root1 = Find(root1);
              root2 
          = Find(root2);
              
          if (root1 == root2) return;

              
          int temp = parent[root1] + parent[root2];
              
          if (parent[root2] < parent[root1])
              {
                  parent[root1] 
          = root2;
                  parent[root2] 
          = temp;
              }
              
          else
              {
                  parent[root2] 
          = root1;
                  parent[root1] 
          = temp;
              }
          }

          void UFSets::Clear(int n)
          {
              
          for (int i = 0; i < n; i++)
                  parent[i] 
          = -1;
          }

          int main()
          {
              UFSets sets(
          30000);
              
          int n, m;
              
          while (true)
              {
                  cin 
          >> n >> m;
                  
          if (n == 0break;
                  sets.Clear(n);
                  
          for (int i = 0; i < m; i++)
                  {
                      
          int t;
                      cin 
          >> t;
                      
          int last, x;
                      cin 
          >> last;
                      last 
          = sets.Find(last);
                      
          for (int j = 1; j < t; j++)
                      {
                          cin 
          >> x;
                          sets.Union(last, x);
                          
          // int temp = sets.Find(x);
                          
          // if (temp != last)
                          
          //     sets.Union(last, temp);
                      }
                  }
                  
          int result = 1;
                  
          int target = sets.Find(0);
                  
          for (int i = 1; i < n; i++)
                      
          if (sets.Find(i) == target)
                          result 
          ++;
                  cout 
          << result << endl;
              }
              
          return 0;
          }


          主站蜘蛛池模板: 金湖县| 亚东县| 类乌齐县| 井陉县| 太仆寺旗| 广宗县| 临湘市| 商都县| 麻江县| 江孜县| 华坪县| 吴忠市| 桃园县| 日土县| 蓬安县| 罗田县| 宁津县| 昌图县| 特克斯县| 屯留县| 临洮县| 孝感市| 辉南县| 四川省| 乌审旗| 奉节县| 沽源县| 泸定县| 承德市| 邵东县| 商洛市| 长丰县| 桃源县| 佛教| 舒城县| 广昌县| 桂平市| 夹江县| 开江县| 仪陇县| 咸丰县|