DANDVIOL - Dandiya Night and Violence

no tags 

It is Dandiya Night! A certain way how dandiya is played is described:

There are N pairs of people playing at a time. Both the person in a pair are playing Dandiya with each other. Since a person might get bored with the same partner, he can swap with a friend in a different pair. For example, if (1, 2) and (3, 4) are initial pairs, and if 1 and 3 are friends, they can swap, and a possible configuration of pairs will be (3, 2) and (1, 4). Friendship relation is transitive in nature. (x,y) and (y, z) friendship pairs imply a (x, z) friendship pair.

Now, Dandiyas are dangerous if not used carefully, and there are always pairs of people who would like to engage in a violent dandiya encounter. A violent dandiya encounter occurs in a pair (5, 6) if 5 and 6 are enemies (not friends). ACM is present at the Dandiya Night and is concerned about this situation.

Given the initial arrangement of pairs, help us to determine the maximum number of violent dandiya encounters possible over the entire Dandiya Night.

Note: A pair (x, y) is unordered, i.e., both (x, y) and (y, x) should be considered the same.


First line denotes number of test cases T.
T test cases follow.
Each test case is formatted as First line consist of integers N, F (N = Number of pairs, F = Number of Friend pairs)
N lines follow, each consisting of two integers, which denote an initial pair of Dandiya Night
(People are numbered from 1 to 2*N)
F lines follow, each denoting a pair of friends.

0<=F<=min(5000, C(2*N, 2)) (C(n, k) = Binomial Coefficient)


For each Test case, output a line consisting of an integer denoting the maximum possible violent dandiya encounters.



2 1
1 2
3 4
1 3
4 3
1 2
3 4
5 6
7 8
1 2
2 3
5 4 Output: 4

Added by:VV
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 JS-MONKEY
Resource:Own problem (CQM 3, BIT Mesra)