[Link] https://www.acmicpc.net/problem/11376
First method and Second method both works. But you might get TLE with second method because the number of candidates of previous matching(from[r_idx]) is bigger.
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
64
65
66
67
68
69
70
71
72
73
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define command_param int argc, char *argv[]
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
const int INF = 987654321;
int n, m;
vector<bool> updated;
vector<vector<int>> edge;
vector<int> from;
vector<int> doing;
bool can_match(int l_idx) {
if(updated[l_idx]) return false;
updated[l_idx] = true;
for(int r_idx: edge[l_idx]) {
if(from[r_idx] == -1 || can_match(from[r_idx])) {
from[r_idx] = l_idx;
return true;
}
}
return false;
}
int solve() {
int cnt = 0;
/* ---First method--- */
for0(i, n) {
for0(iter, 2) {
updated = vector<bool>(n, false);
if(can_match(i)) cnt++;
}
}
/* ---Second method--- */
// for0(iter, 2) {
// for0(i, n) {
// updated = vector<bool>(n, false);
// if(can_match(i)) cnt++;
// }
// }
return cnt;
}
int main() {
FASTIO;
cin >> n >> m;
edge.resize(n << 1);
from.resize(m, -1);
for0(i, n) {
int in; cin >> in;
edge[i].resize(in);
for0(j, edge[i].size()) { cin >> in; edge[i][j] = in - 1; };
}
cout << solve();
return 0;
}