-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax matching.cpp
54 lines (43 loc) · 927 Bytes
/
Max matching.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
48
49
50
51
52
53
54
//#include "pch.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <vector>
#include <math.h>
#define N 500
using namespace std;
typedef long long ll;
int n, m, ans, tmp;
int mt[N], used[N];
vector <int> g[N];
int dfs(int u, int now) {
if (used[u] == now) return 0;
used[u] = now;
for (auto to : g[u]) {
if (mt[to] == -1 || dfs(mt[to], now)) {
mt[to] = u;
return 1;
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
while (cin >> tmp && tmp != 0)
g[i].push_back(tmp - 1);
}
for (int i = 0; i < m; i++) mt[i] = -1;
for (int i = 0; i < n; i++)
dfs(i, i + 1);
for (int i = 0; i < m; i++)
if (mt[i] != -1) ans++;
cout << ans << '\n';
for (int i = 0; i < m; i++)
if (mt[i] != -1)
cout << mt[i] + 1 << " " << i + 1 << '\n';
return 0;
}