题目链接

匈牙利算法模板题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <map>

using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define DEBUG(a) cout << (a) << endl
typedef long long ll;
int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 1}};
int dir4[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int INF = 0x3f3f3f3fLL;
const long long LLF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXn = 1e5 + 15;
const int mod = 1e9 + 7;
//priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;

int cnt,n,m,Next[400];//Next[i]=a表示第i个牛栏中是第a头牛
bool mapn[402][402],mark[400];//mapn[i][j]=true;表示第i头牛喜欢第j个牛栏 mark[i]=true;表示第i个牛栏用过了
bool check(int x)
{
for (int i=1;i<=m;i++)//便利每一个牛栏
if (mapn[x][i]&&(!mark[i]))//如果第x头牛喜欢这个牛栏 并且这个牛栏是空的
{
mark[i]=true;//标记这个牛栏
if (Next[i]==0check(Next[i]))//如果这个牛栏没有牛或者可以腾出地方
{
Next[i]=x;//更新这个牛栏中是第x头牛
return true;
}
}
return false;
}
int main()
{//输入
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
int a;
scanf("%d",&a);
for (int j=1;j<=a;j++)
{
int b;
scanf("%d",&b);
mapn[i][b]=true;
}
}
for (int i=1;i<=n;i++)//遍历每头牛
{
for (int j=1;j<=m;j++)//清空标记数组
mark[j]=false;
if (check(i)) cnt++;
}
printf("%d\n",cnt);
return 0;
}

评论




博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议

载入天数...载入时分秒... 本站使用 Volantis 作为主题 鲁ICP备-20012065号