Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
// Java program for the above approach import java.util.*; // Implementation of DSU class UnionFind { private int[] parent; private int[] rank; private int time; private int count; public UnionFind(int N) { parent = new int[N]; rank = new int[N]; time = 0; count = N; for (int i = 0; i < N; i++) { parent[i] = i; rank[i] = 1; } } public int find(int node) { if (node == parent[node]) return node; return parent[node] = find(parent[node]); } public void performUnion(int u, int v, int updatedTime) { if (count == 1) return; int rootX = find(u); int rootY = find(v); if (rootX != rootY) { if (rank[rootX] < rank[rootY]) { parent[rootX] = rootY; } else if (rank[rootX] > rank[rootY]) { parent[rootY] = rootX; } else { parent[rootX] = rootY; rank[rootY] += 1; } time = updatedTime; count--; } } public int getCount() { return count; } public int getTime() { return time; } } class Main { private static int earliestTime(List
arr, int N) { arr.sort(Comparator.comparingInt(a -> a[2])); UnionFind unionFind = new UnionFind(N); for (int[] it : arr) { unionFind.performUnion(it[0], it[1], it[2]); } return unionFind.getCount() == 1 ? unionFind.getTime() : -1; } public static void main(String[] args) { int N = 6; List
arr = Arrays.asList(new int[][] { { 0, 1, 4 }, { 3, 4, 5 }, { 2, 3, 14 }, { 1, 5, 24 }, { 2, 4, 12 }, { 0, 3, 42 }, { 1, 2, 41 }, { 4, 5, 11 } }); System.out.println(earliestTime(arr, N)); } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)