Cod sursa(job #2467437)

Utilizator educationalLets Go educational Data 4 octombrie 2019 13:38:19
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;
 
#define MaxN 10000
 
vector<int> L[MaxN + 1];
int viz[MaxN + 1];
int st[MaxN + 1];
int dr[MaxN + 1];
int N, M, E, T;
 
bool match(int node)
{
	if(viz[node] == T) return 0;
	viz[node] = T;
 
	for(auto neighbour : L[node])
	if(!dr[neighbour])
	{
		st[node] = neighbour;
		dr[neighbour] = node;
		return 1;	
	}
 
	for(auto neighbour : L[node])
	if(match(dr[neighbour]))
	{
		st[node] = neighbour;
		dr[neighbour] = node;
		return 1;
	}
	
	return 0;
}
 
int main() {
 
	int i, go, x, y, res = 0;
 
	scanf("%d %d %d", &N, &M, &E);
	for(i = 1; i <= E; i++)
	{
		scanf("%d %d", &x, &y);
		L[x].push_back(y);
	}
 
	go = 1;
	while(go)
	{
		++T;
		go = 0;
		for(i = 1; i <= N; i++)
		if(!st[i])
			if(match(i)) go = 1;
	}
 
	for(i = 1; i <= N; i++) res = res + (st[i] > 0);
	printf("%d\n", res);
 
	for(i = 1; i <= N; i++)
	if(st[i]) printf("%d %d\n", i, st[i]);
 
 
return 0;
}