1- class DSU {
2- private int n ;
3- private int [] parentOrSize ;
4-
5- public DSU (int n ){
6- this .n = n ;
7- this .parentOrSize = new int [n ];
8- Arrays .fill (parentOrSize ,-1 );
9- }
10-
11- int merge (int a , int b ){
12- if (!(0 <= a && a < n ) || !(0 <= b && b < n )){
13- // この式が成立しない物は動作しません。
14- return -1 ;
15- }
16- int x = leader (a );
17- int y = leader (b );
18- if (x == y ) return x ;
19- if (-parentOrSize [x ] < -parentOrSize [y ]){
20- int tmp = x ;
21- x = y ;
22- y = tmp ;
23- }
24- parentOrSize [x ] += parentOrSize [y ];
25- parentOrSize [y ] = x ;
26- return x ;
27- }
28-
29- boolean same (int a , int b ){
30- if (!(0 <= a && a < n ) || !(0 <= b && b < n )){
31- // この式が成立しない物は動作しません。
32- return false ;
33- }
34- return leader (a ) == leader (b );
35- }
36-
37- int leader (int a ){
38- if (parentOrSize [a ] < 0 ){
39- return a ;
40- }else {
41- parentOrSize [a ] = leader (parentOrSize [a ]);
42- return parentOrSize [a ];
43- }
44- }
1+ class DSU {
2+ private int n ;
3+ private int [] parentOrSize ;
454
46- int size (int a ) {
47- if (!(0 <= a && a < n )){
48- // この式が成立しない物は動作しません。
49- return -1 ;
50- }
51- return -parentOrSize [leader (a )];
52- }
53-
54- ArrayList <ArrayList <Integer >> groups (){
55- int [] leaderBuf = new int [n ];
56- int [] groupSize = new int [n ];
57- for (int i = 0 ; i < n ; i ++){
58- leaderBuf [i ] = leader (i );
59- groupSize [leaderBuf [i ]]++;
60- }
61- ArrayList <ArrayList <Integer >> result = new ArrayList <ArrayList <Integer >>();
62- for (int i = 0 ; i < n ; i ++){
63- result .add (new ArrayList <>());
64- }
65- for (int i = 0 ; i < n ; i ++){
66- result .get (leaderBuf [i ]).add (i );
67- }
68- return result ;
69- }
70- }
5+ public DSU (int n ) {
6+ this .n = n ;
7+ this .parentOrSize = new int [n ];
8+ Arrays .fill (parentOrSize , -1 );
9+ }
10+
11+ int merge (int a , int b ) {
12+ if (!(0 <= a && a < n ) || !(0 <= b && b < n )) {
13+ // この式が成立しない物は動作しません。
14+ return -1 ;
15+ }
16+ int x = leader (a );
17+ int y = leader (b );
18+ if (x == y ) return x ;
19+ if (-parentOrSize [x ] < -parentOrSize [y ]) {
20+ int tmp = x ;
21+ x = y ;
22+ y = tmp ;
23+ }
24+ parentOrSize [x ] += parentOrSize [y ];
25+ parentOrSize [y ] = x ;
26+ return x ;
27+ }
28+
29+ boolean same (int a , int b ) {
30+ if (!(0 <= a && a < n ) || !(0 <= b && b < n )) {
31+ // この式が成立しない物は動作しません。
32+ return false ;
33+ }
34+ return leader (a ) == leader (b );
35+ }
36+
37+ int leader (int a ) {
38+ if (parentOrSize [a ] < 0 ) {
39+ return a ;
40+ } else {
41+ parentOrSize [a ] = leader (parentOrSize [a ]);
42+ return parentOrSize [a ];
43+ }
44+ }
45+
46+ int size (int a ) {
47+ if (!(0 <= a && a < n )) {
48+ // この式が成立しない物は動作しません。
49+ return -1 ;
50+ }
51+ return -parentOrSize [leader (a )];
52+ }
53+
54+ ArrayList <ArrayList <Integer >> groups () {
55+ int [] leaderBuf = new int [n ];
56+ int [] groupSize = new int [n ];
57+ for (int i = 0 ; i < n ; i ++) {
58+ leaderBuf [i ] = leader (i );
59+ groupSize [leaderBuf [i ]]++;
60+ }
61+ ArrayList <ArrayList <Integer >> result = new ArrayList <ArrayList <Integer >>();
62+ for (int i = 0 ; i < n ; i ++) {
63+ result .add (new ArrayList <>());
64+ }
65+ for (int i = 0 ; i < n ; i ++) {
66+ result .get (leaderBuf [i ]).add (i );
67+ }
68+ return result ;
69+ }
70+ }
0 commit comments