Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Created October 25, 2016 21:26
Show Gist options
  • Save MLLeKander/3137a028dae3edb1fc7962e215d66b4f to your computer and use it in GitHub Desktop.
Save MLLeKander/3137a028dae3edb1fc7962e215d66b4f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
#define VI vector<int>
#define pb push_back
#define NMAX 100000
int l[NMAX], r[NMAX];
VI G[NMAX];
bool visited[NMAX];
int n,m,e,x,y,rs;
bool dfs(int v) {
if(visited[v]) { return 0; }
visited[v]=1;
for(auto w:G[v]) {
if(l[w] == 0 || dfs(l[w])) {
l[w]=v;
r[v]=w;
return 1;
}
}
return 0;
}
int main() {
cin>>n>>m>>e;
for(int i=1; i<=e; ++i) {
cin>>x>>y;
G[x].pb(y+n);
}
bool q=1;
while(q) {
q=0;
memset(visited, 0, sizeof(visited));
for(int i=1; i<=n; ++i) {
if(r[i]==0 && dfs(i)) {
q=1;
rs++;
}
}
}
cout<<rs<<"\n";
}
import java.util.*;
import java.util.stream.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
public class testNode {
public static BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
public static StringTokenizer tokenizer = null;
public static String nextLine() {
try { return buffer.readLine(); } catch (Exception e) { throw new RuntimeException(e); }
}
public static String next() {
while (tokenizer == null || !tokenizer.hasMoreElements()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
public static int nextInt() { return Integer.parseInt(next()); }
public static long nextLong() { return Long.parseLong(next()); }
public static double nextDouble() { return Double.parseDouble(next()); }
public static final Node[] as = new Node[50000];
public static final Node[] bs = new Node[50000];
public static final boolean[] visited = new boolean[50000];
public static void main(String[] args) {
final int n = nextInt(), m = nextInt(), e = nextInt();
for (int i = 0; i < n; i++) { as[i] = new Node(i); }
for (int i = 0; i < n; i++) { bs[i] = new Node(i); }
for (int i = 0; i < e; i++) {
final int x = nextInt()-1, y = nextInt()-1;
as[x].adj.add(bs[y]);
}
boolean flag=true;
int out = 0;
while (flag) {
flag = false;
Arrays.fill(visited, false);
for (int i = 0; i < n; i++) {
if (as[i].match == null && dfs(as[i])) {
flag = true;
out++;
}
}
}
System.out.println(out);
}
public static boolean dfs(Node n) {
if(visited[n.ndx]) { return false; }
visited[n.ndx] = true;
for (Node w : n.adj) {
if(w.match == null || dfs(w.match)) {
w.match=n;
n.match=w;
return true;
}
}
return false;
}
public static class Node {
public ArrayList<Node> adj = new ArrayList<Node>();
public Node match = null;
public int ndx;
public int ndx() { return ndx; }
Node(int ndx_) { ndx=ndx_; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment