[acm-icpc] disjoint set
TRANSCRIPT
![Page 1: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/1.jpg)
Disjoint Set郭至軒(KuoE0)
![Page 2: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/2.jpg)
Latest update: Mar 13, 2013
Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
http://creativecommons.org/licenses/by-sa/3.0/
![Page 3: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/3.jpg)
Disjoint Set中國譯:並查集
1 4
3
785
2
6 9
![Page 4: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/4.jpg)
Disjoint Set中國譯:並查集
1 4
3
785
2
6 9
![Page 5: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/5.jpg)
Disjoint Set中國譯:並查集
1 4
3
785
2
6 9
![Page 6: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/6.jpg)
Disjoint Set中國譯:並查集
1 4
3
785
2
6 9
![Page 7: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/7.jpg)
Tree Structure
1
52
6
4
3
78
91
![Page 8: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/8.jpg)
Tree Structure
1
5
2
6
4
3
7
89
![Page 9: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/10.jpg)
Find Operation
1
5
2
6
node parent
1 1
2 1
5 2
6 2
![Page 11: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/11.jpg)
1
5
2
6
How to determine that node 2 and node 6 in the same set?
![Page 12: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/16.jpg)
...
...
...
...
If the height of tree is very large...
......
} n
![Page 17: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/17.jpg)
Reduce Height
1
5
2
6
![Page 18: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/18.jpg)
Reduce Height
1
5
2
6
1
52
6
![Page 19: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/19.jpg)
Reduce Height
node parent
1 1
2 1
5 1
6 1
1
52
6
![Page 20: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/21.jpg)
Union Operation
1
5
2
6
7
89
Height = 3 Height = 2
![Page 22: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/22.jpg)
Union Operation
1
5
2
6
7
89
Height = 3 Height = 2
![Page 23: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/23.jpg)
Union Operation
1
5
2
6
7
89
Height = 3
![Page 24: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/25.jpg)
Time Complexity
Find Operation O(1)
Union Operation O(1)
![Page 26: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/26.jpg)
Practice Now[UVa] 10583 - Ubiquitous Religions
![Page 27: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/27.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 28: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/28.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 29: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/29.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 30: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/30.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 31: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/31.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 32: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/32.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 33: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/33.jpg)
10583 - Ubiquitous Religions
佛教 佛教
佛教
佛教佛教
基督教
基督教
回教
基督教
![Page 34: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/34.jpg)
10583 - Ubiquitous Religions
佛教
佛教
佛教 佛教
佛教
基督教
基督教
回教
基督教
![Page 35: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/35.jpg)
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 36: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/36.jpg)
Practice Now[UVa] 10608 - Friends
![Page 37: [ACM-ICPC] Disjoint Set](https://reader033.vdocuments.mx/reader033/viewer/2022050906/554ba5c8b4c905ae618b4ec4/html5/thumbnails/37.jpg)
Thank You for Your Listening.