From b1946441b6244b53f377b526c6c444b885214dda Mon Sep 17 00:00:00 2001 From: KOTA SREEVATSA Date: Thu, 27 Feb 2025 15:19:46 +0530 Subject: [PATCH 1/6] Update LCT.mdx added java solution With Link Cut Tree --- content/6_Advanced/LCT.mdx | 137 +++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/content/6_Advanced/LCT.mdx b/content/6_Advanced/LCT.mdx index 73aa24e277..f5b5999ca1 100644 --- a/content/6_Advanced/LCT.mdx +++ b/content/6_Advanced/LCT.mdx @@ -240,6 +240,143 @@ int main() { ``` + + +import java.util.*; + +class Node { + int value; + Node left, right, parent; + boolean reversed; + + Node(int v) { + this.value = v; + } + + void push() { + if (reversed) { + reversed = false; + Node temp = left; + left = right; + right = temp; + if (left != null) left.reversed ^= true; + if (right != null) right.reversed ^= true; + } + } + + boolean isRoot() { + return parent == null || (parent.left != this && parent.right != this); + } +} + +class LCT { + Node[] nodes; + + LCT(int n) { + nodes = new Node[n + 1]; + for (int i = 1; i <= n; i++) nodes[i] = new Node(i); + } + + void rotate(Node child) { + Node parent = child.parent; + Node grandparent = parent.parent; + + if (!parent.isRoot()) { + if (grandparent.right == parent) grandparent.right = child; + else grandparent.left = child; + } + + parent.push(); + child.push(); + + if (parent.left == child) { + parent.left = child.right; + if (parent.left != null) parent.left.parent = parent; + child.right = parent; + } else { + parent.right = child.left; + if (parent.right != null) parent.right.parent = parent; + child.left = parent; + } + + parent.parent = child; + child.parent = grandparent; + } + + void splay(Node node) { + while (!node.isRoot()) { + Node parent = node.parent; + Node grandparent = parent.parent; + if (!parent.isRoot()) rotate((grandparent.right == parent) == (parent.right == node) ? parent : node); + rotate(node); + } + node.push(); + } + + Node access(int v) { + Node last = null; + Node current = nodes[v]; + for (Node p = current; p != null; p = p.parent) { + splay(p); + p.right = last; + last = p; + } + splay(current); + return last; + } + + void makeRoot(int v) { + access(v); + Node current = nodes[v]; + if (current.left != null) { + current.left.reversed ^= true; + current.left = null; + } + } + + void link(int u, int v) { + makeRoot(v); + nodes[v].parent = nodes[u]; + } + + void cut(int u, int v) { + makeRoot(u); + access(v); + if (nodes[v].left != null) { + nodes[v].left.parent = null; + nodes[v].left = null; + } + } + + boolean connected(int u, int v) { + makeRoot(u); + access(v); + return nodes[v].parent != null; + } +} + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + LCT lc = new LCT(n); + + for (int i = 0; i < m; i++) { + String command = sc.next(); + int u = sc.nextInt(); + int v = sc.nextInt(); + + if (command.equals("add")) lc.link(u, v); + else if (command.equals("rem")) lc.cut(u, v); + else if (command.equals("conn")) System.out.println(lc.connected(u, v) ? "YES" : "NO"); + } + + sc.close(); + } +} + + ### With Euler Tour Tree From cf42f70140cbc3a2322ea0b53bce1c5a12d02cef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Feb 2025 09:52:34 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- content/6_Advanced/LCT.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/6_Advanced/LCT.mdx b/content/6_Advanced/LCT.mdx index f5b5999ca1..e41ff9fe07 100644 --- a/content/6_Advanced/LCT.mdx +++ b/content/6_Advanced/LCT.mdx @@ -298,7 +298,7 @@ class LCT { if (parent.right != null) parent.right.parent = parent; child.left = parent; } - + parent.parent = child; child.parent = grandparent; } @@ -366,12 +366,12 @@ public class Main { String command = sc.next(); int u = sc.nextInt(); int v = sc.nextInt(); - + if (command.equals("add")) lc.link(u, v); else if (command.equals("rem")) lc.cut(u, v); else if (command.equals("conn")) System.out.println(lc.connected(u, v) ? "YES" : "NO"); } - + sc.close(); } } From aa02866130042ac1f6ee5761a74d51e5453131e3 Mon Sep 17 00:00:00 2001 From: KOTA SREEVATSA Date: Sun, 2 Mar 2025 11:29:46 +0530 Subject: [PATCH 3/6] Update LCT.mdx updated indentation --- content/6_Advanced/LCT.mdx | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/content/6_Advanced/LCT.mdx b/content/6_Advanced/LCT.mdx index e41ff9fe07..ae3ad40e4d 100644 --- a/content/6_Advanced/LCT.mdx +++ b/content/6_Advanced/LCT.mdx @@ -242,6 +242,7 @@ int main() { + import java.util.*; class Node { @@ -274,7 +275,9 @@ class LCT { LCT(int n) { nodes = new Node[n + 1]; - for (int i = 1; i <= n; i++) nodes[i] = new Node(i); + for (int i = 1; i <= n; i++) { + nodes[i] = new Node(i); + } } void rotate(Node child) { @@ -307,7 +310,9 @@ class LCT { while (!node.isRoot()) { Node parent = node.parent; Node grandparent = parent.parent; - if (!parent.isRoot()) rotate((grandparent.right == parent) == (parent.right == node) ? parent : node); + if (!parent.isRoot()) { + rotate((grandparent.right == parent) == (parent.right == node) ? parent : node); + } rotate(node); } node.push(); @@ -367,15 +372,20 @@ public class Main { int u = sc.nextInt(); int v = sc.nextInt(); - if (command.equals("add")) lc.link(u, v); - else if (command.equals("rem")) lc.cut(u, v); - else if (command.equals("conn")) System.out.println(lc.connected(u, v) ? "YES" : "NO"); + if (command.equals("add")) { + lc.link(u, v); + } else if (command.equals("rem")) { + lc.cut(u, v); + } else if (command.equals("conn")) { + System.out.println(lc.connected(u, v) ? "YES" : "NO"); + } } sc.close(); } } + From e85b55b9e843ae3757880a46c1ba0ee3f202a30a Mon Sep 17 00:00:00 2001 From: Ryan Chou <81596991+ryanchou-dev@users.noreply.github.com> Date: Tue, 4 Mar 2025 22:14:29 -0800 Subject: [PATCH 4/6] code tags --- content/6_Advanced/LCT.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/6_Advanced/LCT.mdx b/content/6_Advanced/LCT.mdx index ae3ad40e4d..19ca524d52 100644 --- a/content/6_Advanced/LCT.mdx +++ b/content/6_Advanced/LCT.mdx @@ -243,6 +243,7 @@ int main() { +``` import java.util.*; class Node { @@ -384,7 +385,7 @@ public class Main { sc.close(); } } - +``` From 4fbafc6f6c4950df3591f63133de575dbf8ce1ed Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Thu, 6 Mar 2025 13:19:22 +0530 Subject: [PATCH 5/6] Update LCT.mdx --- content/6_Advanced/LCT.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/6_Advanced/LCT.mdx b/content/6_Advanced/LCT.mdx index 19ca524d52..c002839ebc 100644 --- a/content/6_Advanced/LCT.mdx +++ b/content/6_Advanced/LCT.mdx @@ -240,10 +240,9 @@ int main() { ``` - -``` +```java import java.util.*; class Node { From 4f488b019d927ed2f8d6faffa41ee4a4da4326c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Mar 2025 07:52:44 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- content/6_Advanced/LCT.mdx | 259 ++++++++++++++++++------------------- 1 file changed, 128 insertions(+), 131 deletions(-) diff --git a/content/6_Advanced/LCT.mdx b/content/6_Advanced/LCT.mdx index c002839ebc..824d89bfa0 100644 --- a/content/6_Advanced/LCT.mdx +++ b/content/6_Advanced/LCT.mdx @@ -246,143 +246,140 @@ int main() { import java.util.*; class Node { - int value; - Node left, right, parent; - boolean reversed; - - Node(int v) { - this.value = v; - } - - void push() { - if (reversed) { - reversed = false; - Node temp = left; - left = right; - right = temp; - if (left != null) left.reversed ^= true; - if (right != null) right.reversed ^= true; - } - } - - boolean isRoot() { - return parent == null || (parent.left != this && parent.right != this); - } + int value; + Node left, right, parent; + boolean reversed; + + Node(int v) { this.value = v; } + + void push() { + if (reversed) { + reversed = false; + Node temp = left; + left = right; + right = temp; + if (left != null) left.reversed ^= true; + if (right != null) right.reversed ^= true; + } + } + + boolean isRoot() { + return parent == null || (parent.left != this && parent.right != this); + } } class LCT { - Node[] nodes; - - LCT(int n) { - nodes = new Node[n + 1]; - for (int i = 1; i <= n; i++) { - nodes[i] = new Node(i); - } - } - - void rotate(Node child) { - Node parent = child.parent; - Node grandparent = parent.parent; - - if (!parent.isRoot()) { - if (grandparent.right == parent) grandparent.right = child; - else grandparent.left = child; - } - - parent.push(); - child.push(); - - if (parent.left == child) { - parent.left = child.right; - if (parent.left != null) parent.left.parent = parent; - child.right = parent; - } else { - parent.right = child.left; - if (parent.right != null) parent.right.parent = parent; - child.left = parent; - } - - parent.parent = child; - child.parent = grandparent; - } - - void splay(Node node) { - while (!node.isRoot()) { - Node parent = node.parent; - Node grandparent = parent.parent; - if (!parent.isRoot()) { - rotate((grandparent.right == parent) == (parent.right == node) ? parent : node); - } - rotate(node); - } - node.push(); - } - - Node access(int v) { - Node last = null; - Node current = nodes[v]; - for (Node p = current; p != null; p = p.parent) { - splay(p); - p.right = last; - last = p; - } - splay(current); - return last; - } - - void makeRoot(int v) { - access(v); - Node current = nodes[v]; - if (current.left != null) { - current.left.reversed ^= true; - current.left = null; - } - } - - void link(int u, int v) { - makeRoot(v); - nodes[v].parent = nodes[u]; - } - - void cut(int u, int v) { - makeRoot(u); - access(v); - if (nodes[v].left != null) { - nodes[v].left.parent = null; - nodes[v].left = null; - } - } - - boolean connected(int u, int v) { - makeRoot(u); - access(v); - return nodes[v].parent != null; - } + Node[] nodes; + + LCT(int n) { + nodes = new Node[n + 1]; + for (int i = 1; i <= n; i++) { nodes[i] = new Node(i); } + } + + void rotate(Node child) { + Node parent = child.parent; + Node grandparent = parent.parent; + + if (!parent.isRoot()) { + if (grandparent.right == parent) grandparent.right = child; + else grandparent.left = child; + } + + parent.push(); + child.push(); + + if (parent.left == child) { + parent.left = child.right; + if (parent.left != null) parent.left.parent = parent; + child.right = parent; + } else { + parent.right = child.left; + if (parent.right != null) parent.right.parent = parent; + child.left = parent; + } + + parent.parent = child; + child.parent = grandparent; + } + + void splay(Node node) { + while (!node.isRoot()) { + Node parent = node.parent; + Node grandparent = parent.parent; + if (!parent.isRoot()) { + rotate((grandparent.right == parent) == (parent.right == node) ? parent + : node); + } + rotate(node); + } + node.push(); + } + + Node access(int v) { + Node last = null; + Node current = nodes[v]; + for (Node p = current; p != null; p = p.parent) { + splay(p); + p.right = last; + last = p; + } + splay(current); + return last; + } + + void makeRoot(int v) { + access(v); + Node current = nodes[v]; + if (current.left != null) { + current.left.reversed ^= true; + current.left = null; + } + } + + void link(int u, int v) { + makeRoot(v); + nodes[v].parent = nodes[u]; + } + + void cut(int u, int v) { + makeRoot(u); + access(v); + if (nodes[v].left != null) { + nodes[v].left.parent = null; + nodes[v].left = null; + } + } + + boolean connected(int u, int v) { + makeRoot(u); + access(v); + return nodes[v].parent != null; + } } public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - int m = sc.nextInt(); - LCT lc = new LCT(n); - - for (int i = 0; i < m; i++) { - String command = sc.next(); - int u = sc.nextInt(); - int v = sc.nextInt(); - - if (command.equals("add")) { - lc.link(u, v); - } else if (command.equals("rem")) { - lc.cut(u, v); - } else if (command.equals("conn")) { - System.out.println(lc.connected(u, v) ? "YES" : "NO"); - } - } - - sc.close(); - } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + LCT lc = new LCT(n); + + for (int i = 0; i < m; i++) { + String command = sc.next(); + int u = sc.nextInt(); + int v = sc.nextInt(); + + if (command.equals("add")) { + lc.link(u, v); + } else if (command.equals("rem")) { + lc.cut(u, v); + } else if (command.equals("conn")) { + System.out.println(lc.connected(u, v) ? "YES" : "NO"); + } + } + + sc.close(); + } } ```