Cod sursa(job #2736123)

Utilizator Rares31100Popa Rares Rares31100 Data 3 aprilie 2021 10:44:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.69 kb
#include <bits/stdc++.h>
#define tooHigh 20001

using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e;
vector <int> gf[20001];
int rez, per[20001];

int nivel[20001], drum[20001];
bool dfs(int nod, int lung = 1)
{
	drum[lung] = nod;

	if(nivel[nod] == 1)
	{
		for(int i = 1; i <= lung; i += 2)
		{
			per[ drum[i] ] = drum[i + 1];
			per[ drum[i + 1] ] = drum[i];
			nivel[ drum[i] ] = tooHigh;
			nivel[ drum[i + 1] ] = tooHigh;
		}

		rez++;
		return true;
	}

	bool found = false;
	if(lung % 2 == 1)
	{
		for(auto vec:gf[nod])
			if(nivel[vec] < nivel[nod])
			{
				found = max(found, dfs(vec, lung + 1));
				if(found)
					break;
			}
	}
	else if(nivel[ per[nod] ] < nivel[nod])
		found = max(found, dfs(per[nod], lung + 1));

	return found;
}
bool bfs()
{
	queue <int> q;

	for(int i = 1; i <= n + m; i++)
		nivel[i] = tooHigh;

	for(int i = 1; i <= n; i++)
		if(!per[i])
		{
			q.push(i);
			nivel[i] = 1;
		}

	bool found = false;
	while(!q.empty())
	{
		int nod = q.front();
		q.pop();

		if(nod <= n)
		{
			for(auto vec:gf[nod])
				if(nivel[vec] == tooHigh)
				{
					nivel[vec] = nivel[nod] + 1;
					q.push(vec);
				}
		}
		else if(!per[nod])
		{
			found = max(found, dfs(nod));
		}
		else
		{
			nivel[ per[nod] ] = nivel[nod] + 1;
			q.push(per[nod]);
		}
	}

	return found;
}

int main()
{
	fin >> n >> m >> e;

	for(int k = 1, i, j; k <= e; k++)
	{
		fin >> i >> j;
		j += n;
		gf[i].push_back(j);
		gf[j].push_back(i);
	}

	while(bfs());

	fout << rez << '\n';

	for(int i = 1; i <= n; i++)
		if(per[i])
			fout << i << ' ' << per[i] - n << '\n';
	
	return 0;
}