Cod sursa(job #2565874)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 2 martie 2020 17:27:05
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lsb(x) (x & (-x))

using namespace std;

const int MAXN = 10000;

vector <int> g[MAXN + 1];
int vis[MAXN + 1], l[MAXN + 1], r[MAXN + 1];
int now;

bool dfs(int nod) {
	for(auto it : g[nod]) {
		if(r[it] == 0) {
			r[it] = nod, l[nod] = it;
			return 1;
		}
	}
	vis[nod] = now;
	for(auto it : g[nod]) {
		if(vis[r[it]] < now && dfs(r[it])) {
			r[it] = nod, l[nod] = it;
			return 1;
		}
	}
	return 0;
}

int main() {
#ifdef HOME
	ifstream cin("A.in");
	ofstream cout("A.out");
#endif
	ifstream cin("cuplaj.in");
	ofstream cout("cuplaj.out");
	int i, a, b, m;
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	cin >> a >> b >> m;
	for(i = 1; i <= m; i++) {
		int x, y; cin >> x >> y;
		g[x].push_back(y);
	}

	bool flag = 1;
	while(flag) {
		now++, flag = 0;
		for(i = 1; i <= a; i++) {
			if(l[i] == 0) {
				flag |= dfs(i);
			}
		}
	}
	
	int ans = 0;
	for(i = 1; i <= a; i++) {
		ans += (l[i] > 0);
	}
	cout << ans << "\n";
	for(i = 1; i <= a; i++) {
		if(l[i] > 0) {
			cout << i << " " << l[i] << "\n";
		}
	}
	
	return 0;
}