Cod sursa(job #1235517)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 29 septembrie 2014 21:48:33
Problema Felinare Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>
#include <bitset>
#include <vector>
#include <cstring>
#define DIM 8200
#define vint vector<int>::iterator
#define infile "felinare.in"
#define outfile "felinare.out"

using namespace std;

ifstream f(infile);
ofstream g(outfile);

vector<int> G[DIM];

int L[DIM], R[DIM];

int n, m, x, y;

bitset<DIM> viz, LS, RS;

bool cuplaj(int nod) {
	if (viz[nod])
		return 0;
	viz[nod] = 1;
	for (vint it = G[nod].begin(); it != G[nod].end(); ++it)
	if (R[*it] == 0) {
		R[*it] = nod;
		L[nod] = *it;
		return 1;
	}
	for (vint it = G[nod].begin(); it != G[nod].end(); ++it)
	if (cuplaj(R[*it])) {
		R[*it] = nod;
		L[nod] = *it;
		return 1;
	}
	return 0;
}

void suport(int nod) {
	for (vint it = G[nod].begin(); it != G[nod].end(); ++it)
	if (!RS[*it]) {
		RS[*it] = 1;
		LS[R[*it]] = 0;
		suport(R[*it]);
	}
}

int main() {
	f >> n >> m;
	for (int i = 1; i <= m; ++i) {
		f >> x >> y;
		G[x].push_back(y);
	}
	bool ok;
	do {
		ok = 0;
		viz &= 0;
		for (int i = 1; i <= n; ++i)
		if (L[i] == 0 && cuplaj(i))
			ok = 1;
	} while (ok);
	int max_cuplaj = 0;
	for (int i = 1; i <= n; ++i)
	if (L[i] != 0) {
		++max_cuplaj;
		LS[i] = 1;
	}
	for (int i = 1; i <= n; ++i)
	if (!LS[i])
		suport(i);
	g << 2 * n - max_cuplaj << "\n";
	for (int i = 1; i <= n; ++i)
	if (LS[i] == 1 && RS[i] == 1)
		g << "0/n";
	else
	if (LS[i] == 0 && RS[i] == 1)
		g << "1\n";
	else
	if (LS[i] == 1 && RS[i] == 0)
		g << "2\n";
	else
		g << "3\n";
	return 0;
}