SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Disjoint Set
    郭至軒(KuoE0)
   KuoE0.tw@gmail.com
        KuoE0.ch
Attribution-ShareAlike 3.0 Unported
           (CC BY-SA 3.0)

  http://creativecommons.org/licenses/by-sa/3.0/

             Latest update: Mar 13, 2013
Disjoint Set
            中國譯:並查集



    1          4

                       7
5                          8
    2
               3
        6              9
Disjoint Set
            中國譯:並查集



    1          4

                       7
5                          8
    2
               3
        6              9
Disjoint Set
            中國譯:並查集



    1          4

                       7
5                          8
    2
               3
        6              9
Disjoint Set
            中國譯:並查集



    1          4

                       7
5                          8
    2
               3
        6              9
Tree Structure


    1          4

                     7
5                        8
    2
               3
         6           9
                     1
Tree Structure

    1          4         7



    2


5         6    3    9        8
Data Structure

 index        0           1           2            3           4

content   parent node parent node parent node parent node parent node



            The parent of root is itself!
Find Operation
            node   parent
    1
             1       1

    2        2       1

5       6    5       2

             6       2
How to determine that node 2 and node 6 in
             the same set?



         1



         2


     5       6
How to determine that node 2 and node 6 in
             the same set?



         1

                       2

         2

                       6
     5       6
How to determine that node 2 and node 6 in
             the same set?



         1

                       2     1

         2

                       6     2
     5       6
How to determine that node 2 and node 6 in
             the same set?



         1

                       2     1

         2

                       6     2    1
     5       6
Source Code

// parent is an array stored the parent
of nodes.

int findSet( int x ) {
    if ( parent[ x ] == x )
      return x;
  return findSet( parent[ x ] );
}
If the height of tree is very large...

              ...




                          }
              ...


        ...         ...
                              n

        ...         ...
Reduce Height

    1



    2


5        6
Reduce Height

    1
                        1


    2
                  5         6
5        6              2
Reduce Height
            node   parent

    1        1       1

             2       1
5       6
    2
             5       1

             6       1
Source Code

// parent is an array stored the parent of
nodes.

int findSet( int x ) {
    if ( parent[ x ] == x )
      return x;
  return parent[ x ] = findSet( parent[ x ] );
}
Union Operation

        1              7



        2


5            6     9       8


    Height = 3    Height = 2
Union Operation

        1              7



        2


5            6     9       8


    Height = 3    Height = 2
Union Operation

             1



     2                7




 5       6       9        8


         Height = 3
Source Code
// parent is an array stored the parent of
nodes.
// height is an array stored the height of tree

void unionSet( int a, int b ) {
  if ( a == b )
    return;
  if ( height[ a ] > height[ b ] )
    parent[ b ] = a;
  else {
    parent[ a ] = b;
    if ( height[ a ] == height[ b ] )
      ++height[ b ];
  }
}
Time Complexity

Find Operation    O(1)



Union Operation   O(1)
Practice Now
[UVa] 10583 - Ubiquitous Religions
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

    佛教       佛教      基督教




    佛教      基督教      佛教




   基督教       佛教      回教
10583 - Ubiquitous Religions

          佛教

                                     回教

佛教                  佛教



     佛教        佛教
                               基督教



                         基督教         基督教
Source Code
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXN 50010
int parent[ MAXN ], height[ MAXN ];

int main() {
   int n, t = 0, e, a, b;
   while ( scanf( “%d %d”, &n, &e ) && n && e ) {
      for ( int i = 1; i <= n; ++i )
          parent[ i ] = i;
          height[ i ] = 1;
      for ( int i = 0; i < e; ++i ) {
          scanf( “%d %d”, &a, &b );
          unionSet( findSet( a ), findSet( b ) );
      }
      int ret = 0;
      for ( int i = 1; i <= n; ++i )
          if ( parent[ i ] == i )
             ++ret;
      printf( “Case %d: %dn”, ++t, ret );
   }
   return 0;
}
Practice Now
 [UVa] 10608 - Friends
Thank You for Your Listening.

Weitere ähnliche Inhalte

Mehr von Chih-Hsuan Kuo

[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
Chih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
Chih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
Chih-Hsuan Kuo
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
Chih-Hsuan Kuo
 
[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching
Chih-Hsuan Kuo
 
[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut
Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
Chih-Hsuan Kuo
 

Mehr von Chih-Hsuan Kuo (20)

Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 
[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
 
[ACM-ICPC] About I/O
[ACM-ICPC] About I/O[ACM-ICPC] About I/O
[ACM-ICPC] About I/O
 
[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching
 
[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 

Kürzlich hochgeladen

10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习
10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习
10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习
PUAXINYEEMoe
 
法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个
法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个
法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个
michaelell902
 

Kürzlich hochgeladen (6)

1.🎉“黑客”如何修改成绩?🤔🎉 在这个信息爆炸的时代,我们经常会看到各种作弊手段。但是你知道吗?有一种作弊方式可能比你想象中更巧妙:它就是——黑客![单...
1.🎉“黑客”如何修改成绩?🤔🎉 在这个信息爆炸的时代,我们经常会看到各种作弊手段。但是你知道吗?有一种作弊方式可能比你想象中更巧妙:它就是——黑客![单...1.🎉“黑客”如何修改成绩?🤔🎉 在这个信息爆炸的时代,我们经常会看到各种作弊手段。但是你知道吗?有一种作弊方式可能比你想象中更巧妙:它就是——黑客![单...
1.🎉“黑客”如何修改成绩?🤔🎉 在这个信息爆炸的时代,我们经常会看到各种作弊手段。但是你知道吗?有一种作弊方式可能比你想象中更巧妙:它就是——黑客![单...
 
taibif_開放資料流程-清理資料01-通則_20240509_20240509.pdf
taibif_開放資料流程-清理資料01-通則_20240509_20240509.pdftaibif_開放資料流程-清理資料01-通則_20240509_20240509.pdf
taibif_開放資料流程-清理資料01-通則_20240509_20240509.pdf
 
啟思中國語文 - 中二 單元一 - 孟嘗君列傳 - 記敍的方法和人稱1.pptx
啟思中國語文 - 中二 單元一 - 孟嘗君列傳 - 記敍的方法和人稱1.pptx啟思中國語文 - 中二 單元一 - 孟嘗君列傳 - 記敍的方法和人稱1.pptx
啟思中國語文 - 中二 單元一 - 孟嘗君列傳 - 記敍的方法和人稱1.pptx
 
taibif_資料標準概念介紹_20240509_20240509_20340509.pdf
taibif_資料標準概念介紹_20240509_20240509_20340509.pdftaibif_資料標準概念介紹_20240509_20240509_20340509.pdf
taibif_資料標準概念介紹_20240509_20240509_20340509.pdf
 
10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习
10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习
10.2.1 马来西亚各州名称的由来六年级历史单元练习马来西亚各州名称的由来练习
 
法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个
法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个
法国蒙彼利埃国家高等建筑学院毕业证制作/德语歌德B1证书/加拿大新斯科舍省农业学院文凭加急制作一个
 

[ACM-ICPC] Disjoint Set

  • 1. Disjoint Set 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  • 2. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ Latest update: Mar 13, 2013
  • 3. Disjoint Set 中國譯:並查集 1 4 7 5 8 2 3 6 9
  • 4. Disjoint Set 中國譯:並查集 1 4 7 5 8 2 3 6 9
  • 5. Disjoint Set 中國譯:並查集 1 4 7 5 8 2 3 6 9
  • 6. Disjoint Set 中國譯:並查集 1 4 7 5 8 2 3 6 9
  • 7. Tree Structure 1 4 7 5 8 2 3 6 9 1
  • 8. Tree Structure 1 4 7 2 5 6 3 9 8
  • 9. Data Structure index 0 1 2 3 4 content parent node parent node parent node parent node parent node The parent of root is itself!
  • 10. Find Operation node parent 1 1 1 2 2 1 5 6 5 2 6 2
  • 11. How to determine that node 2 and node 6 in the same set? 1 2 5 6
  • 12. How to determine that node 2 and node 6 in the same set? 1 2 2 6 5 6
  • 13. How to determine that node 2 and node 6 in the same set? 1 2 1 2 6 2 5 6
  • 14. How to determine that node 2 and node 6 in the same set? 1 2 1 2 6 2 1 5 6
  • 15. Source Code // parent is an array stored the parent of nodes. int findSet( int x ) { if ( parent[ x ] == x ) return x; return findSet( parent[ x ] ); }
  • 16. If the height of tree is very large... ... } ... ... ... n ... ...
  • 17. Reduce Height 1 2 5 6
  • 18. Reduce Height 1 1 2 5 6 5 6 2
  • 19. Reduce Height node parent 1 1 1 2 1 5 6 2 5 1 6 1
  • 20. Source Code // parent is an array stored the parent of nodes. int findSet( int x ) { if ( parent[ x ] == x ) return x; return parent[ x ] = findSet( parent[ x ] ); }
  • 21. Union Operation 1 7 2 5 6 9 8 Height = 3 Height = 2
  • 22. Union Operation 1 7 2 5 6 9 8 Height = 3 Height = 2
  • 23. Union Operation 1 2 7 5 6 9 8 Height = 3
  • 24. Source Code // parent is an array stored the parent of nodes. // height is an array stored the height of tree void unionSet( int a, int b ) { if ( a == b ) return; if ( height[ a ] > height[ b ] ) parent[ b ] = a; else { parent[ a ] = b; if ( height[ a ] == height[ b ] ) ++height[ b ]; } }
  • 25. Time Complexity Find Operation O(1) Union Operation O(1)
  • 26. Practice Now [UVa] 10583 - Ubiquitous Religions
  • 27. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 28. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 29. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 30. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 31. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 32. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 33. 10583 - Ubiquitous Religions 佛教 佛教 基督教 佛教 基督教 佛教 基督教 佛教 回教
  • 34. 10583 - Ubiquitous Religions 佛教 回教 佛教 佛教 佛教 佛教 基督教 基督教 基督教
  • 35. Source Code #include <iostream> #include <cstdio> using namespace std; #define MAXN 50010 int parent[ MAXN ], height[ MAXN ]; int main() { int n, t = 0, e, a, b; while ( scanf( “%d %d”, &n, &e ) && n && e ) { for ( int i = 1; i <= n; ++i ) parent[ i ] = i; height[ i ] = 1; for ( int i = 0; i < e; ++i ) { scanf( “%d %d”, &a, &b ); unionSet( findSet( a ), findSet( b ) ); } int ret = 0; for ( int i = 1; i <= n; ++i ) if ( parent[ i ] == i ) ++ret; printf( “Case %d: %dn”, ++t, ret ); } return 0; }
  • 36. Practice Now [UVa] 10608 - Friends
  • 37. Thank You for Your Listening.