Cod sursa(job #1442738)

Utilizator LegionHagiu Stefan Legion Data 26 mai 2015 09:40:33
Problema Balanta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <iostream>
#include <fstream>
using namespace std;
int stanga, dreapta;
void usor()
{
	ifstream in("balanta.in");
	int a[2050], b[2050], i, j, k, r, n, m, inclus[1050],tot=0,nrsol=0,sol;
	for (i = 1; i <= 1024; i++)
	{
		inclus[i] = 0;
	}
	in >> n;
	in >> m;
	for (i = 1; i <= m; i++)
	{
		in >> k;
		for (j = 1; j <= k; j++)
		{
			in >> a[j];
		}
		for (j = 1; j <= k; j++)
		{
			in >> b[j];
		}
		in >> r;
		if (r == 0)
		{
			for (j = 1; j <= k; j++)
			{
				inclus[a[j]]--;
				inclus[b[j]]--;
			}
		}
		else if (r == 1)
		{
			tot++;
			for (j = 1; j <= k; j++)
			{
				inclus[b[j]]++;
			}
		}
		else if (r == 2)
		{
			tot++;
			for (j = 1; j <= k; j++)
			{
				inclus[a[j]]++;
			}
		}
	}
	for (i = 1; i <= n; i++)
	{
		if (inclus[i] == tot)
		{
			nrsol++;
			sol = i;
		}
	}
	if (nrsol == 1)
	{
		stanga = sol;
	}
	else
	{
		stanga = 0;
	}
}
void greu()
{
	ifstream in("balanta.in");
	int a[2050], b[2050], i, j, k, r, n, m, inclus[1050], tot = 0, nrsol = 0, sol;
	for (i = 1; i <= 1024; i++)
	{
		inclus[i] = 0;
	}
	in >> n;
	in >> m;
	for (i = 1; i <= m; i++)
	{
		in >> k;
		for (j = 1; j <= k; j++)
		{
			in >> a[j];
		}
		for (j = 1; j <= k; j++)
		{
			in >> b[j];
		}
		in >> r;
		if (r == 1){ r = 2; }
		else if (r == 2){ r = 1; }
		if (r == 0)
		{
			for (j = 1; j <= k; j++)
			{
				inclus[a[j]]--;
				inclus[b[j]]--;
			}
		}
		else if (r == 1)
		{
			tot++;
			for (j = 1; j <= k; j++)
			{
				inclus[b[j]]++;
			}
		}
		else if (r == 2)
		{
			tot++;
			for (j = 1; j <= k; j++)
			{
				inclus[a[j]]++;
			}
		}
	}
	for (i = 1; i <= n; i++)
	{
		if (inclus[i] == tot)
		{
			nrsol++;
			sol = i;
		}
	}
	if (nrsol == 1)
	{
		dreapta = sol;
	}
	else
	{
		dreapta = 0;
	}
}
int main()
{
	ofstream out("balanta.out");
	usor();
	greu();
	if (stanga == 0 && dreapta == 0)
	{
		out << "0";
	}
	else if (stanga == 0){ out << dreapta; }
	else if (dreapta == 0){ out << stanga; }
	else if (stanga==dreapta) { out << stanga; }
	else { out << "0"; }
}