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, 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.

a男b女想要组成k对,要从中选择两对不冲突的进行组队,算出有多少方案
比如3男(编号1 ~ 3)4女(编号1 ~ 4)想要组4对 :
(1,2), (1,3), (2,2), (3,4)
以下是可行的方案
(1,3) and (2,2);
(3,4) and (1,3);
以下是不可行的方案
(1,3) and (1,2) — 第一个男孩同时在两组里
(1,2) and (2,2) — 第二个女孩同时在两组里

二、输入

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⋅1e5) — 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⋅1e5.
It is guaranteed that each pair is specified at most once in one test case.

一个整数t,表示测试案例的数量。
每个测试案例的第一行有三个整数a, b, k(1≤a,b,k≤2⋅1e5)
每个测试案例的第二行有k个整数,表示处在第i对的男孩编号(1≤ai≤a)
每个测试案例的第三行有k个整数,表示处在第i对的女孩编号(1≤bi≤b)

三、输出

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

选择符合要求的两对的总方案数。

四、样例

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

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).

五、思路 + 代码

抛去所有的限制条件,假设有k个组队意愿,那么对于其中的每一个意愿都有k - 1个方案来使其与其余任何一对组队。
但是现在要求不能有重复的男孩 / 女孩出现在同一组队中,所以,每新增一对(ai, bi) , 那么在含有ai的对中减一,在含有bi的对中减一,最终得到每队的方案数,再累加。

在这道题目里,因为k个组队意愿中不会有完全相同的两对,所以对于每一对新增的意愿中,不会使[含有ai的对中减一,在含有bi的对中减一]这两个操作同时进行。

代码如下:

#include<iostream>
using namespace std;
typedef long long ll;
//aa[i]表示第i对中男生的编号,bb[i]表示第i对中女生的编号
//ca[i]表示编号为i的男生出现的次数,cb[i]表示编号为i的女生出现的次数
int aa[200005], bb[200005], ca[200005], cb[200005];
void solve() {
	int a, b, k;
	cin >> a >> b >> k;
	memset(aa, 0, sizeof(aa)), memset(bb, 0, sizeof(bb)),
		memset(ca, 0, sizeof(ca)), memset(cb, 0, sizeof(cb));
	//输入a[i] b[i]并计数,题目中的编号是1~n,这里是0~n-1.
	for (int i = 0; i < k; i++) {
		cin >> aa[i];
		aa[i]--;
		ca[aa[i]]++;
	}
	for (int i = 0; i < k; i++) {
		cin >> bb[i];
		bb[i]--;
		cb[bb[i]]++;
	}
	ll ans = 0;
	for (int i = 0; i < k; i++) {
		//扣除重复方案
		ans += (k - 1) - (ca[aa[i]] - 1) - (cb[bb[i]] - 1);
	}
	//所有方案被重复考虑了,需要/2
	//(1,2)和(2,1)看作是一种,(2,1)和(1,2)也被算作一种,
	//但题目要求不重复
	cout << ans / 2 << endl;
}
int main() {
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值