-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorComponentPairs.java
More file actions
94 lines (62 loc) · 2.37 KB
/
Copy pathColorComponentPairs.java
File metadata and controls
94 lines (62 loc) · 2.37 KB
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
import java.util.*;
public class ColorComponentPairs {
static long c = 0; // current component ka size
// DFS: color-wise component ko traverse karega
public static void dfs(int node, List<Integer>[] G, int[] used, int[] parent) {
used[node] = 1; // node ko visited mark karo
c++; // component size badhao
for (int u : G[node]) {
if (used[u] == 0) {
parent[u] = node; // parent set karo
dfs(u, G, used, parent); // recursive DFS call
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // number of nodes
int[][] s = new int[n][2]; // edge list
int[] b = new int[n + 1]; // node colors (1-based indexing)
// Graph banane ke liye adjacency list
List<Integer>[] G = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
G[i] = new ArrayList<>();
}
// Input edges (tree edges)
for (int i = 1; i < n; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
s[i][0] = u;
s[i][1] = v;
}
// Node ke color input lo
for (int i = 1; i <= n; i++) {
b[i] = sc.nextInt();
}
// Graph ko build karo — sirf unhi nodes ko jodo jinke colors same hain
for (int i = 1; i < n; i++) {
int u = s[i][0];
int v = s[i][1];
// agar color same hai to edge lagao
if (b[u] == b[v]) {
G[u].add(v);
G[v].add(u);
}
}
int[] used = new int[n + 1]; // visited array for DFS
int[] parent = new int[n + 1]; // parent tracking
Arrays.fill(parent, -1); // initially -1
long ans = 0; // final answer
// Har unvisited node ke liye DFS chalao
for (int i = 1; i <= n; i++) {
if (used[i] == 0) {
c = 0; // component size reset
dfs(i, G, used, parent);
// har component ka triplet/pair count nikaalo
ans += (c - 1) * (c - 2) / 2;
}
}
System.out.println(ans); // final answer print karo
sc.close(); // Scanner close
}
}