Cod sursa(job #2402114)

Utilizator MaarcellKurt Godel Maarcell Data 10 aprilie 2019 12:48:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 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);}


int N,M,E,l[10100],r[10100];
vi g[10100]; bool v[10100];

bool dfs(int x){
	if (v[x])
		return 0;
		
	v[x] = 1;
	for (int nxt : g[x]){
		if (r[nxt]==0 || dfs(r[nxt])){
			l[x]=nxt,r[nxt]=x;
			return 1;
		}
	}
	
	return 0;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	ifstream cin("cuplaj.in");
	ofstream cout("cuplaj.out");
	cin >> N >> M >> E;
	
	int i;
	for (i=1; i<=E; i++){
		int x,y;
		cin >> x >> y;
		g[x].pb(y);
	}
	
	bool cont=1;
	while (cont){
		cont = 0;
		memset(v,0,sizeof(v));
		for (int i=1; i<=N; i++)
			if (!l[i]){
				cont|=dfs(i);
			}
	}
	
	int res=0;
	for (i=1; i<=N; i++)
		if (l[i])
			res++;
			
	cout << res << "\n";
	for (i=1; i<=N; i++)
		if (l[i])
			cout << i << " " << l[i] << "\n";
}