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 | #include<iostream> #include<cstdlib> #include<cstring> #include<ctime> #include<vector> #include<list> #include<cmath> #include<queue> #include<unordered_set> #include<unordered_map> using namespace std; int n, m, e; vector<int> edges[2005]; bool visited[2005]; int match[2005]; bool biFind(int node) { if (visited[node])return false; visited[node] = true; for (int i = 0; i < edges[node].size(); i++) { if (!match[edges[node][i]] || biFind(match[edges[node][i]])) { match[node] = edges[node][i]; match[edges[node][i]] = node; return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> e; for (int i = 1; i <= e; i++) { int u, v; cin >> u >> v; if (u > n || v > m)continue; edges[u].push_back(v + n); edges[v + n].push_back(u); } int cnter = 0; for (int i = 1; i <= n; i++) { memset(visited, false, sizeof(visited)); if (biFind(i))cnter++; } cout << cnter; } |
洛谷 P3386 【模板】二分图匹配
发表评论