SlideShare uma empresa Scribd logo
1 de 37
Baixar para ler offline
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.

Mais conteúdo relacionado

Mais de Chih-Hsuan Kuo

Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient AlgorithmChih-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-upChih-Hsuan Kuo
 
[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite MatchingChih-Hsuan Kuo
 
[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum CutChih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 

Mais de 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
 

Último

EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxEDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxmekosin001123
 
educ6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxeduc6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxmekosin001123
 
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制jakepaige317
 
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...黑客 接单【TG/微信qoqoqdqd】
 
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书jakepaige317
 
EDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxEDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxmekosin001123
 

Último (6)

EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxEDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
 
educ6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxeduc6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptx
 
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
 
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
 
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
 
EDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxEDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptx
 

[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.