@@ -246,143 +246,140 @@ int main() {
246
246
import java .util .* ;
247
247
248
248
class Node {
249
- int value;
250
- Node left, right, parent;
251
- boolean reversed;
252
-
253
- Node(int v ) {
254
- this .value = v ;
255
- }
256
-
257
- void push() {
258
- if (reversed ) {
259
- reversed = false ;
260
- Node temp = left ;
261
- left = right ;
262
- right = temp ;
263
- if (left != null ) left .reversed ^= true ;
264
- if (right != null ) right .reversed ^= true ;
265
- }
266
- }
267
-
268
- boolean isRoot() {
269
- return parent == null || (parent .left != this && parent .right != this );
270
- }
249
+ int value;
250
+ Node left, right, parent;
251
+ boolean reversed;
252
+
253
+ Node(int v ) { this .value = v ; }
254
+
255
+ void push() {
256
+ if (reversed ) {
257
+ reversed = false ;
258
+ Node temp = left ;
259
+ left = right ;
260
+ right = temp ;
261
+ if (left != null ) left .reversed ^= true ;
262
+ if (right != null ) right .reversed ^= true ;
263
+ }
264
+ }
265
+
266
+ boolean isRoot() {
267
+ return parent == null || (parent .left != this && parent .right != this );
268
+ }
271
269
}
272
270
273
271
class LCT {
274
- Node [] nodes;
275
-
276
- LCT(int n ) {
277
- nodes = new Node [n + 1 ];
278
- for (int i = 1 ; i <= n ; i ++ ) {
279
- nodes [i ] = new Node (i );
280
- }
281
- }
282
-
283
- void rotate(Node child ) {
284
- Node parent = child .parent ;
285
- Node grandparent = parent .parent ;
286
-
287
- if (! parent .isRoot ()) {
288
- if (grandparent .right == parent ) grandparent .right = child ;
289
- else grandparent .left = child ;
290
- }
291
-
292
- parent .push ();
293
- child .push ();
294
-
295
- if (parent .left == child ) {
296
- parent .left = child .right ;
297
- if (parent .left != null ) parent .left .parent = parent ;
298
- child .right = parent ;
299
- } else {
300
- parent .right = child .left ;
301
- if (parent .right != null ) parent .right .parent = parent ;
302
- child .left = parent ;
303
- }
304
-
305
- parent .parent = child ;
306
- child .parent = grandparent ;
307
- }
308
-
309
- void splay(Node node ) {
310
- while (! node .isRoot ()) {
311
- Node parent = node .parent ;
312
- Node grandparent = parent .parent ;
313
- if (! parent .isRoot ()) {
314
- rotate ((grandparent .right == parent ) == (parent .right == node ) ? parent : node );
315
- }
316
- rotate (node );
317
- }
318
- node .push ();
319
- }
320
-
321
- Node access(int v ) {
322
- Node last = null ;
323
- Node current = nodes [v ];
324
- for (Node p = current ; p != null ; p = p .parent ) {
325
- splay (p );
326
- p .right = last ;
327
- last = p ;
328
- }
329
- splay (current );
330
- return last ;
331
- }
332
-
333
- void makeRoot(int v ) {
334
- access (v );
335
- Node current = nodes [v ];
336
- if (current .left != null ) {
337
- current .left .reversed ^= true ;
338
- current .left = null ;
339
- }
340
- }
341
-
342
- void link(int u , int v ) {
343
- makeRoot (v );
344
- nodes [v ].parent = nodes [u ];
345
- }
346
-
347
- void cut(int u , int v ) {
348
- makeRoot (u );
349
- access (v );
350
- if (nodes [v ].left != null ) {
351
- nodes [v ].left .parent = null ;
352
- nodes [v ].left = null ;
353
- }
354
- }
355
-
356
- boolean connected(int u , int v ) {
357
- makeRoot (u );
358
- access (v );
359
- return nodes [v ].parent != null ;
360
- }
272
+ Node [] nodes;
273
+
274
+ LCT(int n ) {
275
+ nodes = new Node [n + 1 ];
276
+ for (int i = 1 ; i <= n ; i ++ ) { nodes [i ] = new Node (i ); }
277
+ }
278
+
279
+ void rotate(Node child ) {
280
+ Node parent = child .parent ;
281
+ Node grandparent = parent .parent ;
282
+
283
+ if (! parent .isRoot ()) {
284
+ if (grandparent .right == parent ) grandparent .right = child ;
285
+ else grandparent .left = child ;
286
+ }
287
+
288
+ parent .push ();
289
+ child .push ();
290
+
291
+ if (parent .left == child ) {
292
+ parent .left = child .right ;
293
+ if (parent .left != null ) parent .left .parent = parent ;
294
+ child .right = parent ;
295
+ } else {
296
+ parent .right = child .left ;
297
+ if (parent .right != null ) parent .right .parent = parent ;
298
+ child .left = parent ;
299
+ }
300
+
301
+ parent .parent = child ;
302
+ child .parent = grandparent ;
303
+ }
304
+
305
+ void splay(Node node ) {
306
+ while (! node .isRoot ()) {
307
+ Node parent = node .parent ;
308
+ Node grandparent = parent .parent ;
309
+ if (! parent .isRoot ()) {
310
+ rotate ((grandparent .right == parent ) == (parent .right == node ) ? parent
311
+ : node );
312
+ }
313
+ rotate (node );
314
+ }
315
+ node .push ();
316
+ }
317
+
318
+ Node access(int v ) {
319
+ Node last = null ;
320
+ Node current = nodes [v ];
321
+ for (Node p = current ; p != null ; p = p .parent ) {
322
+ splay (p );
323
+ p .right = last ;
324
+ last = p ;
325
+ }
326
+ splay (current );
327
+ return last ;
328
+ }
329
+
330
+ void makeRoot(int v ) {
331
+ access (v );
332
+ Node current = nodes [v ];
333
+ if (current .left != null ) {
334
+ current .left .reversed ^= true ;
335
+ current .left = null ;
336
+ }
337
+ }
338
+
339
+ void link(int u , int v ) {
340
+ makeRoot (v );
341
+ nodes [v ].parent = nodes [u ];
342
+ }
343
+
344
+ void cut(int u , int v ) {
345
+ makeRoot (u );
346
+ access (v );
347
+ if (nodes [v ].left != null ) {
348
+ nodes [v ].left .parent = null ;
349
+ nodes [v ].left = null ;
350
+ }
351
+ }
352
+
353
+ boolean connected(int u , int v ) {
354
+ makeRoot (u );
355
+ access (v );
356
+ return nodes [v ].parent != null ;
357
+ }
361
358
}
362
359
363
360
public class Main {
364
- public static void main(String [] args ) {
365
- Scanner sc = new Scanner (System .in );
366
- int n = sc .nextInt ();
367
- int m = sc .nextInt ();
368
- LCT lc = new LCT (n );
369
-
370
- for (int i = 0 ; i < m ; i ++ ) {
371
- String command = sc .next ();
372
- int u = sc .nextInt ();
373
- int v = sc .nextInt ();
374
-
375
- if (command .equals (" add" )) {
376
- lc .link (u , v );
377
- } else if (command .equals (" rem" )) {
378
- lc .cut (u , v );
379
- } else if (command .equals (" conn" )) {
380
- System .out .println (lc .connected (u , v ) ? " YES" : " NO" );
381
- }
382
- }
383
-
384
- sc .close ();
385
- }
361
+ public static void main(String [] args ) {
362
+ Scanner sc = new Scanner (System .in );
363
+ int n = sc .nextInt ();
364
+ int m = sc .nextInt ();
365
+ LCT lc = new LCT (n );
366
+
367
+ for (int i = 0 ; i < m ; i ++ ) {
368
+ String command = sc .next ();
369
+ int u = sc .nextInt ();
370
+ int v = sc .nextInt ();
371
+
372
+ if (command .equals (" add" )) {
373
+ lc .link (u , v );
374
+ } else if (command .equals (" rem" )) {
375
+ lc .cut (u , v );
376
+ } else if (command .equals (" conn" )) {
377
+ System .out .println (lc .connected (u , v ) ? " YES" : " NO" );
378
+ }
379
+ }
380
+
381
+ sc .close ();
382
+ }
386
383
}
387
384
` ` `
388
385
0 commit comments