链接:https://www.nowcoder.com/acm/contest/143/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Nowcoder University has 4n students and n dormitories ( Four students per dormitory). Students numbered from 1 to 4n.
And in the first year, the i-th dormitory 's students are (x1[i],x2[i],x3[i],x4[i]), now in the second year, Students need to decide who to live with.
In the second year, you get n tables such as (y1,y2,y3,y4) denote these four students want to live together.
Now you need to decide which dormitory everyone lives in to minimize the number of students who change dormitory.
输入描述:
The first line has one integer n.
Then there are n lines, each line has four integers (x1,x2,x3,x4) denote these four students live together in the first year
Then there are n lines, each line has four integers (y1,y2,y3,y4) denote these four students want to live together in the second year
输出描述:
Output the least number of students need to change dormitory.
示例1
输入
2
1 2 3 4
5 6 7 8
4 6 7 8
1 2 3 5
输出
2
说明
Just swap 4 and 5
备注:
1<=n<=100
1<=x1,x2,x3,x4,y1,y2,y3,y4<=4n
It's guaranteed that no student will live in more than one dormitories.
题目大意:
有n间宿舍,每一间有4n个学生,第一年学校安排住宿方式
第二年换宿舍是学生自己组合的,
现在给出第一年和第二年的住宿方式,问换宿舍人数最少是多少。
思路:
一开始想用并查集记录有多少最多有多少学生不用换宿舍,但是如果有两个宿舍都最多有2个人不用换宿舍,但是这两组人是住在同一个宿舍的话,并查集的做法就会出问题
例如给出
3
1 2 3 4
5 6 7 8
9 10 11 12
1 2 7 8
3 5 11 12
4 6 9 10
并查集的做法是6个人,但是实际上是7个人。
也就是说不能单纯根据最后捆绑的人越多就不动,因为宿舍与宿舍之间也是有联系的。
想到组与组之间有联系的话就突然想到这其实就是一个带权二分图的匹配问题。
这里我的做法是反过来想,要使得最少的人改变宿舍,就相当于使最多的人不改变宿舍
那么我们对第一年的宿舍i和第二年的宿舍j连边,权值为宿舍i变成宿舍j最多能保留多少个学生。
对这个二分图求最大匹配得出的权值就是最多有多少学生不改变宿舍
这个结果用4n减去就是所求答案
这里我用的是km模板,当然也是可以用费用流怼过去的。
#include
#include
#include
#include
#include