cf1475C. Ball in Berland

在一所学校,毕业典礼上有一个舞会,每个班级需要提供两对男女参与。已知有a个男生、b个女生和k对愿意跳舞的男女组合。任务是找到满足条件的不同方式数,即选出的两对中没有任何人重复。题目提供了每对男女的编号,并要求输出符合条件的组合数。解题思路涉及二分图概念,通过计算每个选定的第一对男女后,第二对可选的数量来得出答案。
摘要由CSDN通过智能技术生成

At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.

Each class must present two couples to the ball. In Vasya’s class, a boys and b girls wish to participate. But not all boys and not all girls are ready to dance in pairs.

Formally, you know k possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.

For example, if a=3, b=4, k=4 and the couples (1,2), (1,3), (2,2), (3,4) are ready to dance together (in each pair, the boy’s number comes first, then the girl’s number), then the following combinations of two pairs are possible (not all possible options are listed below):

(1,3) and (2,2);
(3,4) and (1,3);
But the following combinations are not possible:

(1,3) and (1,2) — the first boy enters two pairs;
(1,2) and (2,2) — the second girl enters two pairs;
Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains three integers a, b and k (1≤a,b,k≤2⋅105) — the number of boys and girls in the class and the number of couples ready to dance together.

The second line of each test case contains k integers a1,a2,…ak. (1≤ai≤a), where ai is the number of the boy in the pair with the number i.

The third line of each test case contains k integers b1,b2,…bk. (1≤bi≤b), where bi is the number of the girl in the pair with the number i.

It is guaranteed that the sums of a, b, and k over all test cases do not exceed 2⋅105.

It is guaranteed that each pair is specified at most once in one test case.

Output
For each test case, on a separate line print one integer — the number of ways to choose two pairs that match the condition above.

Example
inputCopy
3
3 4 4
1 1 2 3
2 3 2 4
1 1 1
1
1
2 2 4
1 1 2 2
1 2 1 2
outputCopy
4
0
2
Note
In the first test case, the following combinations of pairs fit:

(1,2) and (3,4);
(1,3) and (2,2);
(1,3) and (3,4);
(2,2) and (3,4).
There is only one pair in the second test case.

In the third test case, the following combinations of pairs fit:

(1,1) and (2,2);
(1,2) and (2,1).

题意
a个男生,b个女生,k对一起跳舞的男女,需要选出2对一起跳舞的出来,这两对不能出现相同的男生或女生

思路
首先想到的自然是暴力,遍历第一对男女,然后再遍历一次符合题目要求的第二对男女,这样的时间复杂度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljw0925

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值