/**
* Problem: LeetCode 3600 - Maximize Spanning Tree Stability with Upgrades
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
static class DSU {
int[] parent;
int[] rank;
int components;
DSU(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
components = n;
}
int find(int i) {
int root = i;
while (root != parent[root]) {
root = parent[root];
}
int curr = i;
while (curr != root) {
int next = parent[curr];
parent[curr] = root;
curr = next;
}
return root;
}
boolean union(int i, int j) {
int rootI = find(i);
int rootJ = find(j);
if (rootI != rootJ) {
if (rank[rootI] < rank[rootJ]) {
parent[rootI] = rootJ;
} else if (rank[rootI] > rank[rootJ]) {
parent[rootJ] = rootI;
} else {
parent[rootJ] = rootI;
rank[rootI]++;
}
components--;
return true;
}
return false;
}
}
public int maxStability(int n, int[][] edges, int k) {
DSU mandatoryDsu = new DSU(n);
int mandatoryCount = 0;
int minMandatory = Integer.MAX_VALUE;
// Validate mandatory edges
for (int[] e : edges) {
if (e[3] == 1) {
if (!mandatoryDsu.union(e[0], e[1])) {
return -1; // Cycle in mandatory edges
}
mandatoryCount++;
minMandatory = Math.min(minMandatory, e[2]);
}
}
// Validate whole graph connectivity
DSU totalDsu = new DSU(n);
for (int[] e : edges) {
totalDsu.union(e[0], e[1]);
}
if (totalDsu.components > 1) {
return -1;
}
int low = 1;
int high = mandatoryCount > 0 ? Math.min(200000, minMandatory) : 200000;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (canFormTree(n, edges, k, mid)) {
ans = mid;
low = mid + 1; // Search for higher stability
} else {
high = mid - 1;
}
}
return ans;
}
private boolean canFormTree(int n, int[][] edges, int k, int target) {
DSU dsu = new DSU(n);
// 1. Mandatory edges must satisfy strength >= target
for (int[] e : edges) {
if (e[3] == 1) {
if (e[2] < target) {
return false;
}
dsu.union(e[0], e[1]);
}
}
// 2. Add cost 0 optional edges (strength >= target)
for (int[] e : edges) {
if (e[3] == 0 && e[2] >= target) {
dsu.union(e[0], e[1]);
}
}
// 3. Add cost 1 optional edges (strength < target and 2 * strength >= target)
int upgradesUsed = 0;
for (int[] e : edges) {
if (e[3] == 0 && e[2] < target && 2 * e[2] >= target) {
if (dsu.find(e[0]) != dsu.find(e[1])) {
if (upgradesUsed < k) {
dsu.union(e[0], e[1]);
upgradesUsed++;
}
}
}
}
return dsu.components == 1;
}
}
"""
Problem: LeetCode 3600 - Maximize Spanning Tree Stability with Upgrades
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
from typing import List
class DSU:
def __init__(self, n: int):
self.parent = list(range(n))
self.rank = [0] * n
self.components = n
def find(self, i: int) -> int:
root = i
while root != self.parent[root]:
root = self.parent[root]
curr = i
while curr != root:
nxt = self.parent[curr]
self.parent[curr] = root
curr = nxt
return root
def union(self, i: int, j: int) -> bool:
root_i = self.find(i)
root_j = self.find(j)
if root_i != root_j:
if self.rank[root_i] < self.rank[root_j]:
self.parent[root_i] = root_j
elif self.rank[root_i] > self.rank[root_j]:
self.parent[root_j] = root_i
else:
self.parent[root_j] = root_i
self.rank[root_i] += 1
self.components -= 1
return True
return False
class Solution:
def maxStability(self, n: int, edges: List[List[int]], k: int) -> int:
mandatory_dsu = DSU(n)
mandatory_count = 0
min_mandatory = float('inf')
# Check mandatory edges for cycles
for u, v, s, must in edges:
if must == 1:
if not mandatory_dsu.union(u, v):
return -1
mandatory_count += 1
min_mandatory = min(min_mandatory, s)
# Check overall graph connectivity
total_dsu = DSU(n)
for u, v, s, must in edges:
total_dsu.union(u, v)
if total_dsu.components > 1:
return -1
low = 1
high = min(200000, min_mandatory) if mandatory_count > 0 else 200000
ans = -1
def can_form_tree(target: int) -> bool:
dsu = DSU(n)
# 1. Mandatory edges must have strength >= target
for u, v, s, must in edges:
if must == 1:
if s < target:
return False
dsu.union(u, v)
# 2. Cost 0 optional edges
for u, v, s, must in edges:
if must == 0 and s >= target:
dsu.union(u, v)
# 3. Cost 1 optional edges (up to k)
upgrades_used = 0
for u, v, s, must in edges:
if must == 0 and s < target <= 2 * s:
if dsu.find(u) != dsu.find(v):
if upgrades_used < k:
dsu.union(u, v)
upgrades_used += 1
return dsu.components == 1
while low <= high:
mid = (low + high) // 2
if can_form_tree(mid):
ans = mid
low = mid + 1
else:
high = mid - 1
return ans
/**
* Problem: LeetCode 3600 - Maximize Spanning Tree Stability with Upgrades
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
struct DSU {
vector<int> parent;
vector<int> rank;
int components;
DSU(int n) : parent(n), rank(n, 0), components(n) {
for (int i = 0; i < n; i++) parent[i] = i;
}
int find(int i) {
int root = i;
while (root != parent[root]) root = parent[root];
int curr = i;
while (curr != root) {
int next = parent[curr];
parent[curr] = root;
curr = next;
}
return root;
}
bool unite(int i, int j) {
int rootI = find(i);
int rootJ = find(j);
if (rootI != rootJ) {
if (rank[rootI] < rank[rootJ]) {
parent[rootI] = rootJ;
} else if (rank[rootI] > rank[rootJ]) {
parent[rootJ] = rootI;
} else {
parent[rootJ] = rootI;
rank[rootI]++;
}
components--;
return true;
}
return false;
}
};
bool canFormTree(int n, const vector<vector<int>>& edges, int k, int target) {
DSU dsu(n);
// 1. Mandatory edges
for (const auto& e : edges) {
if (e[3] == 1) {
if (e[2] < target) return false;
dsu.unite(e[0], e[1]);
}
}
// 2. Cost 0 optional edges (strength >= target)
for (const auto& e : edges) {
if (e[3] == 0 && e[2] >= target) {
dsu.unite(e[0], e[1]);
}
}
// 3. Cost 1 optional edges (strength < target and 2 * strength >= target)
int upgradesUsed = 0;
for (const auto& e : edges) {
if (e[3] == 0 && e[2] < target && 2 * e[2] >= target) {
if (dsu.find(e[0]) != dsu.find(e[1])) {
if (upgradesUsed < k) {
dsu.unite(e[0], e[1]);
upgradesUsed++;
}
}
}
}
return dsu.components == 1;
}
public:
int maxStability(int n, vector<vector<int>>& edges, int k) {
DSU mandatoryDsu(n);
int mandatoryCount = 0;
int minMandatory = INT_MAX;
for (const auto& e : edges) {
if (e[3] == 1) {
if (!mandatoryDsu.unite(e[0], e[1])) {
return -1; // Cycle in mandatory edges
}
mandatoryCount++;
minMandatory = min(minMandatory, e[2]);
}
}
DSU totalDsu(n);
for (const auto& e : edges) {
totalDsu.unite(e[0], e[1]);
}
if (totalDsu.components > 1) return -1;
int low = 1;
int high = mandatoryCount > 0 ? min(200000, minMandatory) : 200000;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (canFormTree(n, edges, k, mid)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}
};
/**
* Problem: LeetCode 3600 - Maximize Spanning Tree Stability with Upgrades
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
class DSU {
/**
* @param {number} n
*/
constructor(n) {
this.parent = new Int32Array(n);
this.rank = new Int8Array(n);
for (let i = 0; i < n; i++) {
this.parent[i] = i;
}
this.components = n;
}
/**
* @param {number} i
* @return {number}
*/
find(i) {
let root = i;
while (root !== this.parent[root]) {
root = this.parent[root];
}
let curr = i;
while (curr !== root) {
let next = this.parent[curr];
this.parent[curr] = root;
curr = next;
}
return root;
}
/**
* @param {number} i
* @param {number} j
* @return {boolean}
*/
union(i, j) {
let rootI = this.find(i);
let rootJ = this.find(j);
if (rootI !== rootJ) {
if (this.rank[rootI] < this.rank[rootJ]) {
this.parent[rootI] = rootJ;
} else if (this.rank[rootI] > this.rank[rootJ]) {
this.parent[rootJ] = rootI;
} else {
this.parent[rootJ] = rootI;
this.rank[rootI]++;
}
this.components--;
return true;
}
return false;
}
}
/**
* @param {number} n
* @param {number[][]} edges
* @param {number} k
* @return {number}
*/
var maxStability = function(n, edges, k) {
const mandatoryDsu = new DSU(n);
let mandatoryCount = 0;
let minMandatory = Infinity;
// Validate mandatory edges
for (let i = 0; i < edges.length; i++) {
const [u, v, s, must] = edges[i];
if (must === 1) {
if (!mandatoryDsu.union(u, v)) {
return -1; // Cycle detected in mandatory edges
}
mandatoryCount++;
if (s < minMandatory) {
minMandatory = s;
}
}
}
// Validate overall graph connectivity
const totalDsu = new DSU(n);
for (let i = 0; i < edges.length; i++) {
totalDsu.union(edges[i][0], edges[i][1]);
}
if (totalDsu.components > 1) {
return -1;
}
let low = 1;
let high = mandatoryCount > 0 ? Math.min(200000, minMandatory) : 200000;
let ans = -1;
function canFormTree(target) {
const dsu = new DSU(n);
// 1. Mandatory edges
for (let i = 0; i < edges.length; i++) {
const [u, v, s, must] = edges[i];
if (must === 1) {
if (s < target) return false;
dsu.union(u, v);
}
}
// 2. Cost 0 optional edges
for (let i = 0; i < edges.length; i++) {
const [u, v, s, must] = edges[i];
if (must === 0 && s >= target) {
dsu.union(u, v);
}
}
// 3. Cost 1 optional edges (up to k)
let upgradesUsed = 0;
for (let i = 0; i < edges.length; i++) {
const [u, v, s, must] = edges[i];
if (must === 0 && s < target && 2 * s >= target) {
if (dsu.find(u) !== dsu.find(v)) {
if (upgradesUsed < k) {
dsu.union(u, v);
upgradesUsed++;
}
}
}
}
return dsu.components === 1;
}
while (low <= high) {
const mid = (low + high) >> 1;
if (canFormTree(mid)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
};