Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- using namespace std;
- const int maxn = 305;
- int graph[maxn][maxn];
- int n, m;
- bool visited[maxn];
- int match[maxn]; // i --> match[i]
- bool dfs(int node) {
- for(int i = 0; i < maxn; i++) {
- if(graph[node][i] == 1 and !visited[i]) {
- visited[i] = true;
- if(match[i] == -1 or dfs(match[i])) {
- match[i] = node;
- return true;
- }
- }
- }
- return false;
- }
- int maximum_bipartite_matching() {
- memset(match, -1, sizeof match);
- int result = 0;
- for(int i = 0; i < n; i++) {
- memset(visited, false, sizeof visited);
- if(dfs(i)) {
- result++;
- }
- }
- return result;
- }
- int main() {
- cin >> n >> m;
- memset(graph, 0, sizeof graph);
- for(int i = 0; i < n; i++) {
- int a, b;
- cin >> a >> b;
- graph[a][b] = 1;
- }
- cout << maximum_bipartite_matching() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement