Cod sursa(job #2149718)

Utilizator MaarcellKurt Godel Maarcell Data 2 martie 2018 21:58:58
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.46 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <ctime>
#include <unordered_map>
#include <iomanip>
#include <complex>
#include <cassert>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define deb(a) cerr<< #a << "= " << (a)<<"\n";

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
typedef complex<double> base;
typedef vector<int> vi;
typedef pair<int,int> pii;

template<class T> ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream; }
ll fpow(ll x, ll p, ll m){ll r=1; for (;p;p>>=1){ if (p&1) r=r*x%m; x=x*x%m; } return r;}
ll inv(ll a, ll b){ return a<2 ? a : ((a-inv(b%a,a))*b+1)/a%b; }
int gcd(int a, int b){ if (!b) return a; return gcd(b,a%b);}
ll gcd(ll a, ll b){ if (!b) return a; return gcd(b,a%b);}


struct edge{
	int to,cap,f;
	edge(int to=0, int cap=0, int f=0) : to(to),cap(cap),f(f) {}
};

int N,M,ptr[20010],lvl[20010],q[20010],K; vector<int> g[20010];
vector<edge> e;

void add_edge(int x, int y, int cap){
	g[x].pb(e.size());
	e.pb(edge(y,cap,0));
	g[y].pb(e.size());
	e.pb(edge(x,0,0));
}

bool bfs(){
	memset(lvl,0,N*sizeof(lvl[0]));
	lvl[0]=1;
	q[0]=0;
	K=1;
	
	for (int i=0; i<K; i++){
		int x=q[i];
		
		for (int id : g[x]){
			edge ed=e[id];
			
			if (!lvl[ed.to] && ed.cap-ed.f>0){
				lvl[ed.to]=lvl[x]+1;
				q[K++]=ed.to;
			}
		}
	}
	
	return lvl[N-1]>0;
}

int dfs(int nod, int mcap){
	if (nod==N-1) 
		return mcap;
		
	for (int &p=ptr[nod]; p<g[nod].size(); p++){
		
		edge ed=e[g[nod][p]];
		
		if (lvl[ed.to]==lvl[nod]+1 && ed.cap>ed.f){
			int v=dfs(ed.to,min(mcap,ed.cap-ed.f));
			
			if (v>0){
				e[g[nod][p]].f+=v;
				e[g[nod][p]^1].f-=v;
				return v;
			}
		}
	}
	
	return 0;
}

int dinic(){
	int flow=0;
	
	while (bfs()){
		
		memset(ptr,0,N*sizeof(ptr[0]));
		
		while (int v=dfs(0,(1<<30)))
			flow+=v;
	}
	
	return flow;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	
	int L,R;
	
	cin >> L >> R >> M; 
	
	int i;
	for (i=1; i<=M; i++){
		int x,y;
		cin >> x >> y;
		add_edge(x,L+y,1);
	}
	
	N=R+L+2;
	for (i=1; i<=L; i++)
		add_edge(0,i,1);
	
	for (i=1; i<=R; i++)
		add_edge(L+i,R+L+1,1);
		
	cout << dinic() << "\n";
	
	return 0;
}