-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path24_BipartiteMatching.cpp
47 lines (43 loc) Β· 910 Bytes
/
24_BipartiteMatching.cpp
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
#include <iostream>
#include <vector>
#define MAX 101
using namespace std;
vector<int> a[MAX];
int d[MAX];
bool c[MAX];
int n = 3, m;
// 맀μΉμ μ±κ³΅ν κ²½μ° True, μ€ν¨ν κ²½μ° False
bool dfs(int x) {
// μ°κ²°λ λͺ¨λ λ
Έλμ λν΄μ λ€μ΄κ° μ μλ μλ
for (int i = 0; i < a[x].size(); i++) {
int t = a[x][i];
// μ΄λ―Έ μ²λ¦¬ν λ
Έλλ λ μ΄μ λ³Ό νμκ° μμ
if (c[t]) continue;
c[t] = true;
// λΉμ΄μκ±°λ μ μ λ
Έλμ λ λ€μ΄κ° 곡κ°μ΄ μλ κ²½μ°
if (d[t] == 0 || dfs(d[t])) {
d[t] = x;
return true;
}
}
return false;
}
int main(void) {
a[1].push_back(1);
a[1].push_back(2);
a[1].push_back(3);
a[2].push_back(1);
a[3].push_back(2);
int count = 0;
for (int i = 1; i <= n; i++) {
fill(c, c + MAX, false);
if (dfs(i)) count++;
}
printf("%dκ°μ 맀μΉμ΄ μ΄λ£¨μ΄μ‘μ΅λλ€.\n", count);
for (int i = 1; i < MAX; i++) {
if (d[i] != 0) {
printf("%d -> %d\n", d[i], i);
}
}
return 0;
}