Codeforces 1475C - Ball in Berland

Insert picture description here
General idea

The Tao problem gives us a boy and b girl. There are k pairs of combinations. Let us find out how many combinations are in which no one has appeared twice.

Topic analysis

According to the principle of tolerance and exclusion, we record that each boy appears cnt1[i] times in the pairing, and each girl appears cnt2[i] times in the pairing. Then we traverse the k pairs. For each pair, another The number of options that a pair can choose is: k-cnt1[i]-cnt2[i] + 1 , minus the number of groups the boy is in and the number of groups the girl is in. This boy and girl are subtracted twice, so plus 1.

#include <iostream>

using namespace std;

const int N = 2e5+10;

pair<int,int> edges[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 >> edges[i].first , cnt1[edges[i].first]++;
	for(int i = 1; i <= k; i++) cin >> edges[i].second , cnt2[edges[i].second]++;
	long long res = 0;
	for(int i = 1; i <= k; i++) 
	{
    
    
		int x = edges[i].first , y = edges[i].second;
		res += k - cnt1[x] - cnt2[y] + 1;
	}
	cout << res / 2 << endl;
}

int main()
{
    
    
	int t; cin >> t;
	while(t--) solve();
	return 0; 
}

Guess you like

Origin blog.csdn.net/cosx_/article/details/113759937