

You are given an undirected graph with βNβ vertices and βMβ edges. Each edge has a particular type as described below:
An edge with type 1 can only be traversed by a person βAβ.
An edge with type 2 can only be traversed by a person βBβ.
An edge with type 3 can be traversed by both persons βAβ and βBβ.
Your task is to determine the maximum number of edges that can be removed such that after removing these edges, all the nodes can still be traversed by both βAβ and βBβ.
The first line contains an integer βTβ, which denotes the number of test cases to be run. Then, the βTβ test cases follow.
The first line of each test case contains two space-separated integers, βNβ, denoting the total number of nodes, and βMβ, denoting the total number of edges.
Then βMβ lines follow. Each of which has three space-separated integers, βXβ βYβ, and βZβ.
βXβ denotes the type of edge. βYβ and βZβ denote the two nodes between which the edge is present.
Output Format:
For each test case, print a single line containing a single integer denoting the maximum number of edges that can be removed.
The output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N,M < 10 ^ 4
edges[i].length = 3
1 <= edges[i][0] <= 3
1 <= edges[i][1], edges[i][1] <= N
Where βTβ is the number of test cases, βNβ is the total number of nodes, βMβ is the total number of edges and edges[i] describes the i-th edge
Time Limit: 1 sec.
1
4 6
1 3 4
1 1 2
2 1 3
2 2 4
3 1 4
3 2 3
2
In the first case, there are a total of 2 typeA edges. One of them can be removed as the graph will still be traversable. Similarly, one of the two typeB edges can be removed as well because even after removing an edge, B can still visit all nodes using any other path. So, a total of 2 edges can be removed, and all nodes can still be visited by both βAβ and βBβ.
1
4 3
1 1 4
2 1 2
3 2 3
-1
In this case, there is a path for βAβ between node 1 and node 4. There is also a path between node 2 and node 3. But there is no connection between these two paths. Hence βAβ can never visit all nodes.
Similarly, for βBβ, there is a path between node 1 and node 2. There is also a path between node 2 and node 3. But there is no path to reach node 4, and thus, there is no connection between node 4 and any other node. Hence βAβ can never visit all nodes.
Build the network instead of removing extra edges.
Approach
The approach is to use Union-Find to find out the minimum number of edges that are required to make the graph a single connected component for both βAβ and βBβ. Since using an edge of type 3 can minimize the total number of edges needed, we will consider these first. Finally, after considering all the edges, if the entire graph can be traversed by both A and B, we print the number of edges that were not needed, else we print β-1β.
Steps
class UnionFind {
O(M), where βMβ is the total number of edges.
The time complexity is O(M) because there are a total of βMβ edges, and for each edge, we are checking if this edge is required or not.
O(N), where βNβ is the total number of nodes.
We need extra linear space to keep track of which nodes can be traversed by A and which nodes can be traversed by B.