Ball in Berland 容斥初体验

Ball in Berland

 CodeForces - 1475C 

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值