Codeforces Round 697 – Ball in Berland Solution

Codeforces Round 697 – Ball in Berland Solution

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.

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

Solution :

// <-- Coded by Pasindu_Piumal -->
#include <bits/stdc++.h> 
using namespace std;
 
#define ll long long int
#define ld long double
#define str string 
#define F first
#define S second 
#define all(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
 
const int mxN=1e5;
ll a, b, k;
vector<pair<int, int>> cpl;
 
void solve() {
 cin >> a >> b >> k;
 cpl.resize(k);
 vector<int> fir(a+2), sec(b+2);
 for(int i=0;i<k;i++)cin >> cpl[i].F;
 for(int i=0;i<k;i++)cin >> cpl[i].S;
 for(int i=0;i<k;i++)fir[cpl[i].F]++;
   for(int i=0;i<k;i++)sec[cpl[i].S]++;
 ll ans=0; 
 for(int i=0;i<k;i++) {
 ans+=k-fir[cpl[i].F]-sec[cpl[i].S]+1;
 }
 cout << ans/2 << "\n";
} 
 
int main() {
 ios_base::sync_with_stdio(0);
 cin.tie(0);
 
 int t;
 cin >> t;
 while(t--) {
 solve();
 }
 
 return 0;
}

 112 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *