Cod sursa(job #1549550)

Utilizator bogdan0707Matei Bogdan bogdan0707 Data 12 decembrie 2015 14:34:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;

int N, M, E,C[20000],marked[20000],l[10000];
vector<int> V[20000];

int DFS(int i)
{
	marked[i] = 1;
	for (int j = 0; j < l[i]; j++)
	{
		int k = V[i][j];
		if (marked[k] != 0 || C[i] == k)
			continue;
		if (C[k] == -1)
		{
			marked[k] = 1;
			C[i] = k;
			C[k] = i;
			return 1;
		}
		else {
			marked[k] = 1;
			if (DFS(C[k]) != 0)
			{
				C[i] = k;
				C[k] = i;
				return 1;
			}
			else continue;
		}
	}
	return 0;
}

void resetMark()
{
	for (int j = 0; j < 20000; j++)
		marked[j] = 0;
}

int main()
{
	int i, k=0, x, y;
	bool trail = true;
	ifstream fileInput; fileInput.open("cuplaj.in");
	fileInput >> N >> M >> E;
	fileInput >> x >> y;
	y += N;
	V[x].push_back(y);
	V[y].push_back(x);
	l[x]++;
	l[y]++;
	for (i = 0; i < E-1; i++)
	{
		fileInput >> x >> y;
		y += N;
		V[x].push_back(y);
		V[y].push_back(x);
		l[x]++;
		l[y]++;
	}
	fileInput.close();
	for (i = 0; i < 20000; i++)
		C[i] = -1;
	while (trail == true)
	{
		trail = false;
		resetMark();
		for (i = 0; i <= N; i++)
			if(marked[i]==0 && C[i]==-1)
				if (DFS(i) != 0)
				{
					trail = true;
					k++;
				}
	}
	ofstream fileOutput; fileOutput.open("cuplaj.out");
	fileOutput << k << "\n";
	for (i = 0; i <= N; i++)
		if (C[i] != -1)
			fileOutput << i << " " << C[i] - N << "\n";
	fileOutput.close();
	return 0;
}