一、题目

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

Ball in Berland相关推荐

  1. Codeforces Round #697 (Div. 3)A~G解题报告

    Codeforces Round #697 (Div. 3)A~G解题报告 题 A Odd Divisor 题目介绍 解题思路 乍一想本题,感觉有点迷迷糊糊,但是证难则反,直接考虑没有奇数因子的情况, ...

  2. 贪心题集(vjoj)

    首先总结一下贪心,算法贪心算法(重点就是一个贪字)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解,而每个局部最优解的总和就 ...

  3. Codeforces Round #697 (Div. 3) (Unrated for Div. 3)全部题解

    题目链接:https://codeforces.com/contest/1475 文章目录 A.Odd Divisor B.New Year's Number C.Ball in Berland D. ...

  4. Codeforces Round #697 (Div. 3)

    目录 A - Odd Divisor B - New Year's Number C - Ball in Berland D - Cleaning the Phone E - Advertising ...

  5. CUDA Samples: green ball

    以下CUDA sample是分别用C++和CUDA实现的生成的绿色的球图像,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程CUDA实战>一书的第五章,各个文件内容 ...

  6. Aizu - 0033 Ball

    这题书上写让用DFS--可是这一比较就出来啊-- Ball Aizu - 0033 図のように二股に分かれている容器があります.1 から 10 までの番号が付けられた10 個の玉を容器の開口部 A か ...

  7. AOJ0033 Ball【贪心+序列处理】

    図のように二股に分かれている容器があります.1 から 10 までの番号が付けられた10 個の玉を容器の開口部 A から落とし.左の筒 B か右の筒 C に玉を入れます.板 D は支点 E を中心に左右 ...

  8. codeforces808G Anthem of Berland(kmp+自动机+dp)

    先计算s+#的前缀函数,然后再计算出在 a-z字符集内的自动机.然后使用动态规则计算. dp(i,j)表示在文本串t的第i个字符,自动机状态为j(即前缀函数值)时的s在t出现的最大次数.当第i+1个字 ...

  9. hdu-4811 Ball

    题目链接: Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

最新文章

  1. 温泉关一役历史资料(电影:斯巴达300勇士)
  2. Windows Server 2003 R2中的“分布式文件系统”案例应用
  3. spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)
  4. 内容营销的21条黄金法则
  5. 【LaTeX】E喵的LaTeX新手入门教程(5)参考文献、文档组织
  6. 小技巧,找出所有check table设置为某个数据库表的数据库表
  7. java中true转换为int_在Java中将字节转换为int的最优雅的方式
  8. oracle for net,使用Oracle Developer Tools For Visual Studio .NET-.NET教程,数据库应用
  9. 即时通讯学习笔记004---即时通讯服务器种类认知
  10. Linux下virtualenv与virtualenvwrapper详解
  11. jenkins忘记密码和常用插件的下载
  12. jq select 操作
  13. mysql用 fifo 记录日志_Python学习第四十七天记录打call:mysqlclient操作MySQL关系型数据库...
  14. 解决java环境变量配置不生效
  15. 基于单片机GSM的防火防盗系统的设计
  16. 设置MyEclipse2015黑色主题背景及删除主题
  17. 姓杜起名:杜姓高雅霸气的男孩名字
  18. 工作流(activiti7)-简单的介绍和使用(一)
  19. 阿基米德螺旋线原理及代码
  20. VCS(DVE)调试

热门文章

  1. 【C语言学习笔记】26. 指针(3)指向指针的指针、传递指针给函数
  2. 盘点人工智能重点技术领域
  3. 初探redis:redis集群的数据分区和故障转移
  4. 雅虎微软合作对手机搜索领域的影响
  5. C# Revit二次开发基础/核心编程---建筑建模-标高和轴网
  6. int型整数的数值范围
  7. 刘利刚老师谈什么是计算机图形学?
  8. XML是什么?有什么用?
  9. 学习Oracle数据库入门到精通教程资料合集
  10. Python爬虫入门教程07:腾讯视频弹幕爬取