37
Disjoint Set 郭至軒(KuoE0[email protected] KuoE0.ch

[ACM-ICPC] Disjoint Set

Embed Size (px)

Citation preview

Page 3: [ACM-ICPC] Disjoint Set

Disjoint Set中國譯:並查集

1 4

3

785

2

6 9

Page 4: [ACM-ICPC] Disjoint Set

Disjoint Set中國譯:並查集

1 4

3

785

2

6 9

Page 5: [ACM-ICPC] Disjoint Set

Disjoint Set中國譯:並查集

1 4

3

785

2

6 9

Page 6: [ACM-ICPC] Disjoint Set

Disjoint Set中國譯:並查集

1 4

3

785

2

6 9

Page 7: [ACM-ICPC] Disjoint Set

Tree Structure

1

52

6

4

3

78

91

Page 8: [ACM-ICPC] Disjoint Set

Tree Structure

1

5

2

6

4

3

7

89

Page 9: [ACM-ICPC] Disjoint Set

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!

Page 10: [ACM-ICPC] Disjoint Set

Find Operation

1

5

2

6

node parent

1 1

2 1

5 2

6 2

Page 11: [ACM-ICPC] Disjoint Set

1

5

2

6

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

Page 12: [ACM-ICPC] Disjoint Set

1

5

2

6

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

6

2

Page 13: [ACM-ICPC] Disjoint Set

1

5

2

6

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

6 2

2 1

Page 14: [ACM-ICPC] Disjoint Set

1

5

2

6

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

6 2

2 1

1

Page 15: [ACM-ICPC] Disjoint Set

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 ] );}

Page 16: [ACM-ICPC] Disjoint Set

...

...

...

...

If the height of tree is very large...

......

} n

Page 17: [ACM-ICPC] Disjoint Set

Reduce Height

1

5

2

6

Page 18: [ACM-ICPC] Disjoint Set

Reduce Height

1

5

2

6

1

52

6

Page 19: [ACM-ICPC] Disjoint Set

Reduce Height

node parent

1 1

2 1

5 1

6 1

1

52

6

Page 20: [ACM-ICPC] Disjoint Set

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 ] );}

Page 21: [ACM-ICPC] Disjoint Set

Union Operation

1

5

2

6

7

89

Height = 3 Height = 2

Page 22: [ACM-ICPC] Disjoint Set

Union Operation

1

5

2

6

7

89

Height = 3 Height = 2

Page 23: [ACM-ICPC] Disjoint Set

Union Operation

1

5

2

6

7

89

Height = 3

Page 24: [ACM-ICPC] Disjoint Set

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 ];}

}

Page 25: [ACM-ICPC] Disjoint Set

Time Complexity

Find Operation O(1)

Union Operation O(1)

Page 27: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 28: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 29: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 30: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 31: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 32: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 33: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教 佛教

佛教

佛教佛教

基督教

基督教

回教

基督教

Page 34: [ACM-ICPC] Disjoint Set

10583 - Ubiquitous Religions

佛教

佛教

佛教 佛教

佛教

基督教

基督教

回教

基督教

Page 35: [ACM-ICPC] Disjoint Set

Source Code#include <iostream>#include <cstdio>using namespace std;#define MAXN 50010int 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: %d\n”, ++t, ret );}return 0;

}

Page 37: [ACM-ICPC] Disjoint Set

Thank You for Your Listening.