Cod sursa(job #2082819)

Utilizator Juve45UAIC Alexandru Ionita Juve45 Data 6 decembrie 2017 20:01:50
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>

#define dbg(x) cerr<<#x": "<<x<<"\n"
#define dbg_p(x) cerr<<#x": "<<x.first<<","<<x.second<<"\n"
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

#define DMAX 1
#define NMAX 1
#define MMAX 1

using namespace std;

int n, k, x,m,e,u,v;
vector<int> vx[10001],vy[10001];
int viz[10001], px[10001], py[10001];

bool dfs(int n)
{
	viz[n]=1;
	for (auto v: vx[n])
		if (py[v]==0||(!viz[py[v]]&&dfs(py[v])))
		{
			px[n]=v;
			py[v]=n;
			return true;
		}
		return false;
}

int main()
{
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin>>n>>m>>e;
	for (int i=1;i<=e;i++)
			{
				cin>>u>>v;
				vx[u].push_back(v);
				vy[v].push_back(u);
			}
	int ok = 1;
	while (ok)
	{
		ok = 0;
		memset(viz,0, sizeof(viz));
		for (int i=1;i<=n;i++)
		{
			if (viz[i]==0 && !px[i])
			{
				if (dfs(i))
				{
					ok = 1;
					break;
				}
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; i++)
		if(px[i])
			ans++;
	cout << ans << '\n';
	for(int i = 1; i <= n; i++)
		if(px[i])
			cout << i << ' ' << px[i] << '\n';
	return 0;
}