#K11321. Count Pairs of Nodes with Same Color in a Graph
Count Pairs of Nodes with Same Color in a Graph
Count Pairs of Nodes with Same Color in a Graph
You are given an undirected graph with n nodes and m edges. Each node is assigned a color, and the colors are given as integers. Your task is to count the number of pairs of nodes (u, v) that meet the following conditions:
- There is a path between u and v (i.e., they are in the same connected component).
- Both nodes have the same color.
For any connected component and for each color within that component, if there are c nodes of that color, the number of valid pairs within that group is given by:
$\displaystyle \frac{c(c-1)}{2}$
Calculate the sum of such pairs over all connected components and all colors.
inputFormat
The first line contains two integers n and m separated by a space, where n is the number of nodes and m is the number of edges.
The second line contains n integers representing the colors of the nodes. The colors are 1-indexed.
The following m lines each contain two integers u and v, representing an edge between nodes u and v.
Input is provided via standard input (stdin).
outputFormat
Output a single integer, which is the total number of pairs of nodes with the same color and in the same connected component.
Output should be directed to standard output (stdout).
## sample4 3
1 2 1 2
1 2
2 3
3 4
2