(补题 HDU 1068 )Girls and Boys(二分图最大匹配)
题目大意
给你n个学生,编号从\(0\sim{n-1}\),接下来每一行代表编号为\(i\)学生的“浪漫”关系人数以及其对应的学生编号\(S_i\),让你求出被包含在“浪漫关系”中最大的人数(就是有哪些人相互之间有关系)
Sample Input
7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
Sample Output
5
2
解题思路
不多说先上图
loading......
代码样例
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
int e[maxn][maxn];
int match[maxn];
bool book[maxn];
int n;
bool dfs(int u)
{
for (int i = 0; i < n; i++)
{
if (book[i] == false && e[u][i] == 1)
{
book[i] = true;
if (match[i] == 0 || dfs(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
}
int main()
{
while (~scanf("%d", &n))
{
int sum = 0;
memset(e, 0, sizeof(e));
memset(match, 0, sizeof(match));
for (int i = 0; i < n; i++)
{
int fr, m;
char a,b,c,f;
scanf("%d%c%c",&fr,&a,&f);
scanf("%c%d%c",&b,&m,&c);
// cout << f << " " << m << endl;
for (int j = 0; j < m; j++)
{
int to;
scanf("%d", &to);
e[fr][to] = 1;
}
}
for (int i = 0; i < n; i++)
{
memset(book, false, sizeof(book));
if (dfs(i))
sum++;
}
printf("%d\n", n - sum / 2);
}
return 0;
}