Codeforces Round #697 (Div. 3) C. Ball in Berland

C. Ball in Berland
Time limit per test:2 seconds
Memory limit per test:256 megabytes

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

Input:
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
Output:
4
0
2

题目描述

a:男生个数(每个男生编号为1~a)
b:女生个数(每个女生编号为1~b)
k:男女搭配的对数
题目的意思是,每次选两对上场,不能出现同一个人上场多次。问一共能有多少种选派方案。

思路

题目中说道,不会出现重复的一对。
所以可以用两个数组分别记录每个男生出现的次数和每个女生出现的次数。
在选定一对(x,y)后,第二对出现的情况数就是k-cx(男生x出现的次数)-cy(女生y出现的次数)+1
最后累加然后除二即是最终结果。
除二是因为无先后顺序。(题目要的是组合不是排列)

AC代码

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 2e5 + 5;
int arra[N], arrb[N];
int v1[N], v2[N];
int main(void)
{
	int t, a, b, k;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &a, &b, &k);
		for (int i = 0; i <= a; ++i) v1[i] = 0;
		for (int i = 0; i <= b; ++i) v2[i] = 0;
		for (int i = 0; i < k; ++i) scanf("%d", &arra[i]);
		for (int i = 0; i < k; ++i) scanf("%d", &arrb[i]);
		for (int i = 0; i < k; ++i) {
			++v1[arra[i]]; // 与点arra[i]有关的边加1
			++v2[arrb[i]]; // 与点arrb[i]有关的边加1
		}
		long long count = 0;
		for (int i = 0; i < k; ++i) {
			count += k - v1[arra[i]] - v2[arrb[i]] + 1;
		}
		printf("%lld\n", count/2);
	}
 
 
	return 0;
}
上一篇:面向对象编程-对象和类


下一篇:insert table 和create table as 区别