-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhungarian.cpp
More file actions
42 lines (37 loc) · 902 Bytes
/
Copy pathhungarian.cpp
File metadata and controls
42 lines (37 loc) · 902 Bytes
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
/* Author: NogiNonoka
Date: 2021.1.13
Time Complexity: O(VE) */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
struct Hungarian {
int n, m;
bool graph[MAXN][MAXN];
int lnk[MAXN];
bool vis[MAXN];
void Init() {
memset(graph, 0, sizeof(graph));
memset(lnk, 0, sizeof(lnk));
memset(vis, 0, sizeof(vis));
}
bool Dfs(int x) {
for (int i = 1; i <= n; i++) {
if (graph[x][i] && !vis[i]) {
vis[i] = true;
if (lnk[i] == 0 || Dfs(lnk[i])) {
lnk[i] = x;
return true;
}
}
}
return false;
}
int hungarian() {
int ans = 0;
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (Dfs(i)) ans++;
}
return ans;
}
} hgr;