10
10
/**
11
11
* http://codeforces.com/contest/566/problem/D
12
12
* http://codeforces.com/contest/566
13
+ *
14
+ *
15
+ * Tutorial
16
+ * http://codeforces.com/blog/entry/19518
17
+ * Resolver os problemas desse tutorial futuramente
18
+ *
19
+ * Solucao interessante
20
+ * https://github.com/yancouto/maratona-sua-mae/blob/master/Yan/codeforces/566D.cpp
21
+ *
13
22
* */
14
23
public class RestructuringCompany {
15
24
25
+ private static final BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (System .in ));
26
+ private static final PrintWriter printWriter = new PrintWriter (new OutputStreamWriter (System .out ), true );
16
27
17
- public static int [] teams ;
18
- public static int [] sz ;
19
- public static int [] next ;
28
+ private static int [] teams ;
29
+ private static int [] sz ;
30
+ private static int [] next ;
20
31
21
- public static void init (int size ) {
32
+ private static void init (int size ) {
22
33
teams = new int [size +1 ];
23
34
sz = new int [size +1 ];
24
35
next = new int [size +1 ];
@@ -29,13 +40,11 @@ public static void init(int size) {
29
40
}
30
41
}
31
42
32
- public static void union (int p , int q ) {
33
- int rootP = find (p );
34
- int rootQ = find (q );
43
+ private static void unionByRank (int p , int q ) {
44
+ int rootP = findWithPathCompression (p );
45
+ int rootQ = findWithPathCompression (q );
35
46
if (rootP == rootQ )
36
47
return ;;
37
- teams [rootP ] = rootQ ;
38
- /*
39
48
// weight compare
40
49
if (sz [rootP ] < sz [rootQ ]) {
41
50
teams [rootP ] = rootQ ;
@@ -45,24 +54,58 @@ public static void union(int p, int q) {
45
54
teams [rootQ ] = rootP ;
46
55
sz [rootP ] += sz [rootQ ];
47
56
}
48
- */
49
57
}
50
58
51
- public static boolean hasSameRoot (int p , int q ) {
59
+ private static void union (int p , int q ) {
60
+ int rootP = find (p );
61
+ int rootQ = find (q );
62
+ if (rootP == rootQ )
63
+ return ;;
64
+ teams [rootP ] = rootQ ;
65
+ }
66
+
67
+ private static boolean hasSameRoot (int p , int q ) {
52
68
return find (p ) == find (q );
53
69
}
54
70
55
- public static int find (int p ) {
71
+ private static int findWithPathCompression (int p ) {
72
+ if (p == teams [p ])
73
+ return p ;
74
+ teams [p ] = teams [teams [p ]];
75
+ return findWithPathCompression (teams [p ]);
76
+ }
77
+
78
+ private static int find (int p ) {
56
79
if (p == teams [p ])
57
80
return p ;
58
- teams [p ] = teams [teams [p ]]; // path compression
59
81
return find (teams [p ]);
60
82
}
61
83
84
+ // TLE
85
+ private static void solver1 (int type , int x , int y ) {
86
+ if (type == 1 ) {
87
+ union (x , y );
88
+ }
89
+ else if (type == 2 ) {
90
+ for (int j = x +1 ; j <= y ;) {
91
+ union (j -1 , j );
92
+ int tempRoot = next [j ];
93
+ next [j ] = next [y ];
94
+ j = tempRoot ;
95
+ }
96
+ }
97
+ else {
98
+ printWriter .printf ("%s\n " , hasSameRoot (x , y ) ? "YES" : "NO" );
99
+ }
100
+ }
101
+
102
+ private static void solver2 (int type , int x , int y ) {
103
+
104
+ }
105
+
62
106
63
107
public static void main (String [] args ) {
64
- BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (System .in ));
65
- PrintWriter printWriter = new PrintWriter (new OutputStreamWriter (System .out ), true );
108
+
66
109
try {
67
110
StringTokenizer tk = new StringTokenizer (bufferedReader .readLine (), " " );
68
111
int n = Integer .parseInt (tk .nextToken ());
@@ -73,21 +116,16 @@ public static void main(String[] args) {
73
116
int type = Integer .parseInt (tk .nextToken ());
74
117
int x = Integer .parseInt (tk .nextToken ());
75
118
int y = Integer .parseInt (tk .nextToken ());
76
- if (type == 1 ) {
77
- union (x , y );
78
- }
79
- else if (type == 2 ) {
80
- int i =x +1 ;
81
- while (i <=y ) {
82
- union (i , x );
83
- int tempRoot = next [i ];
84
- next [i ] = next [y ];
85
- i = tempRoot ;
86
- }
87
- }
88
- else {
89
- printWriter .printf ("%s\n " , hasSameRoot (x , y ) ? "YES" : "NO" );
90
- }
119
+
120
+ /**
121
+ * type == tipo de operacao na estrutura union-findWithPathCompression
122
+ * Tipo 1 e 2 sao unioes, tipo 3 verifica se o NÓ x possui o mesmo NÓ-PAI de y
123
+ *
124
+ * Union tipo 1 - muda o no pai de teams[y] para o no pai de teams[x
125
+ * Union tipo 2 - muda o no pai dos teams[x+1, x+2, x+3 ... y] para o no pai de teams[x]
126
+ *
127
+ * */
128
+ solver1 (type , x , y );
91
129
}
92
130
} catch (Exception e ) {}
93
131
}
0 commit comments