forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
125 lines (95 loc) · 3.46 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class DisjointSet {
DisjointSet parent = this;
int size = 1;
DisjointSet findRoot() {
if (parent != this) {
// path compression
parent = parent.findRoot();
}
return parent;
}
void union(DisjointSet other) {
if (other == this) return;
DisjointSet root = findRoot();
DisjointSet otherRoot = other.findRoot();
if (otherRoot == root) return;
// union by size
if (root.size >= otherRoot.size) {
otherRoot.parent = root;
root.size += otherRoot.size;
}
else {
root.parent = otherRoot;
otherRoot.size += root.size;
}
}
}
public class solution {
final static long mod = 1_000_000_007;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
DisjointSet[] components = new DisjointSet[n + 1];
for (int i = 0; i < n - 1; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
char color = scanner.next().charAt(0);
// ignore red edges
if (color == 'r') continue;
DisjointSet aComponent = createComponentIfNeeded(components, a);
DisjointSet bComponent = createComponentIfNeeded(components, b);
aComponent.union(bComponent);
}
// uniquify components
Set<DisjointSet> uniqueComponents = new HashSet<>();
for (int i = 0; i < n; i++) {
DisjointSet component = components[i];
if (component != null) {
uniqueComponents.add(component.findRoot());
}
}
long validTriplets = choose3from(n); // all possible triplets
for (DisjointSet component : uniqueComponents) {
// subtract all triplets within the components
validTriplets -= choose3from(component.size);
// subtract all triplets build from 2 vertices of the components and 1 other vertex
validTriplets -= choose2from(component.size) * (n - component.size);
}
// apply modulo for some reason beyond my imagination
System.out.println(validTriplets % mod);
}
// lazily create a component
static DisjointSet createComponentIfNeeded(DisjointSet[] components, int index) {
if (components[index] == null) {
components[index] = new DisjointSet();
}
return components[index];
}
// optimized calculation of binom(n, 3)
static long choose3from(int n) {
if (n < 3) return 0;
// k = 3
// n! / ( k! (n-k)! ) = (n-k+1) * ... * n / k! = (n - 2) * ... * n / 6
long res = 1;
for (int x = n - 2; x <= n; x++) {
res *= x;
}
return res / 6;
}
// optimized calculation of binom(n, 2)
static long choose2from(int n) {
if (n < 2) return 0;
// k = 2
// n! / (k! (n-k)!) = (n-k+1) * ... * n / k! = (n - 1) * ... * n / 2
long res = 1;
for (int x = n - 1; x <= n; x++) {
res *= x;
}
return res / 2;
}
}