Ball in Berland
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, aa boys and bb girls wish to participate. But not all boys and not all girls are ready to dance in pairs.
Formally, you know kk 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=3a=3, b=4b=4, k=4k=4 and the couples (1, 2)(1,2), (1, 3)(1,3), (2, 2)(2,2), (3, 4)(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)(1,3) and (2, 2)(2,2);
- (3, 4)(3,4) and (1, 3)(1,3);
But the following combinations are not possible:
- (1, 3)(1,3) and (1, 2)(1,2) — the first boy enters two pairs;
- (1, 2)(1,2) and (2, 2)(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.
一共有k对,那么当前选中的一对就有k-1对和它搭配,而要求一个男生或者女生不能同时出现在同一对,所以每次计算就要减去那个男生或者女生的出现的对数(算出当前这对可以有多少种方案最后累加),由于同时减去了男生和女生所以要加1,最后ans计算了重复的对数,所以去重除以2。
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
int s[N], m[N];
void solve()
{
int cnt1[N] = { 0 }, cnt2[N] = { 0 };
int a, b, k;
cin >> a >> b >> k;
for (int i = 1; i <= k; i++)
{
cin >> s[i], cnt1[s[i]]++;
}
for (int i = 1; i <= k; i++)
{
cin >> m[i], cnt2[m[i]]++;
}
long long res = 0;
for (int i = 0; i < k; i++)
{
res += k - cnt1[i] - cnt2[i] + 1;
}
res = res / 2;
cout << res << endl;
}
int main()
{
int t; cin >> t;
while (t--) solve();
return 0;
}