spoj#DANDVIOL. Dandiya Night and Violence
Dandiya Night and Violence
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.
Input
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.
T<=100
1<=N<=200
0<=F<=min(5000, C(2*N, 2)) (C(n, k) = Binomial Coefficient)
Output
For each Test case, output a line consisting of an integer denoting the maximum possible violent dandiya encounters.
Example
Input:
2
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
9