@@ -16,7 +16,7 @@ class A{
16
16
private InputStream inputStream ;
17
17
private OutputStream outputStream ;
18
18
private FastReader in ;
19
- private PrintWriter out ;
19
+ private PrintWriter out ;
20
20
/*
21
21
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
22
22
@@ -47,12 +47,12 @@ public A(boolean stdIO)throws FileNotFoundException{
47
47
48
48
}
49
49
50
- void run ()throws Exception {
50
+ void run ()throws Exception {
51
51
52
52
// int tests = i();
53
53
// once();
54
- // for(int t = 1 ; t<= tests ; t++){
55
54
int n = i (); int m = i ();
55
+ // for(int t = 1 ; t<= tests ; t++){
56
56
init (n );
57
57
for (int q = 1 ; q <= m ; q ++){
58
58
int type = i ();
@@ -61,7 +61,6 @@ void run()throws Exception{
61
61
int a = i (); int b = i ();
62
62
join (a ,b );
63
63
64
-
65
64
}else {
66
65
int u = i ();
67
66
out .write ("root of " +u +"is :" +root (u )+"\n " );
@@ -75,39 +74,38 @@ void once(){
75
74
76
75
}
77
76
78
- int f [] = new int [200005 ];
79
- int h [] = new int [200005 ];
77
+ int f [] = new int [200005 ];
78
+ int h [] = new int [200005 ];
80
79
81
80
void init (int n ){
82
- for (int i = 1 ; i <= n ; i ++){
83
- f [i ] = i ;
84
- h [i ] = 0 ;
85
- }
81
+ for (int i = 1 ; i <= n ; i ++){
82
+ f [i ] = i ;
83
+ h [i ] = 0 ;
84
+ }
86
85
}
86
+
87
87
int root (int i ){
88
-
89
- if (f [i ] != i )
90
- f [i ] = root (f [i ]);
91
-
92
- return f [i ];
88
+ if (f [i ] != i )
89
+ f [i ] = root (f [i ]);
90
+ return f [i ];
93
91
}
92
+
94
93
void join (int x , int y ){
95
- int xroot = root (x );
96
- int yroot = root (y );
97
- if (h [xroot ] < h [yroot ])
98
- f [xroot ] = yroot ;
99
- else if (h [xroot ] > h [yroot ])
100
- f [yroot ] = xroot ;
101
- else {
102
- f [yroot ] = xroot ;
103
- h [xroot ]++;
104
- }
105
- }
106
-
107
- //****************************** My Utilities ***********************//
94
+ int xroot = root (x );
95
+ int yroot = root (y );
96
+ if (h [xroot ] < h [yroot ])
97
+ f [xroot ] = yroot ;
98
+ else if (h [xroot ] > h [yroot ])
99
+ f [yroot ] = xroot ;
100
+ else {
101
+ f [yroot ] = xroot ;
102
+ h [xroot ]++;
103
+ }
104
+ }
105
+
108
106
void print_r (Object ...o ){
109
- out .write ("\n " +Arrays .deepToString (o )+"\n " );
110
- out .flush ();
107
+ out .write ("\n " +Arrays .deepToString (o )+"\n " );
108
+ out .flush ();
111
109
}
112
110
113
111
int hash (String s ){
0 commit comments