Cod sursa(job #831174)

Utilizator moscaliuc_ovidiuMoscaliuc Ovidiu moscaliuc_ovidiu Data 8 decembrie 2012 11:16:43
Problema Felinare Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <fstream>
#include <vector>
#include <set>

#define MAX_N	((const unsigned int)8192)

typedef std::vector<int>						Neighbours;

class Node
{
public:
	int in;
	int out;
	Neighbours inn;
	Neighbours outn;
	bool bOutFel;
	bool bInFel;

public:
	Node() :
		in(0), out(0), bInFel(true), bOutFel(true)
	{

	}

	int gradMax() const
	{
		if (in > out)
			return in;
		else
			return out;
	}

	bool operator < (const Node &_other) const
	{
		return this->gradMax() > _other.gradMax();
	}
};

Node Graf[MAX_N];

class NodeComparator
{
public:
	bool operator()(const int &_node1, const int &_node2) const
	{
		return Graf[_node1] < Graf[_node2];
	}
};

typedef std::multiset<int, NodeComparator>		NodeSet;

NodeSet nodes;

void compute()
{

}

int main()
{
	std::ifstream fin("felinare.in");
	std::ofstream fout("felinare.out");

	int N, M, x, y;

	fin>>N>>M;

	int sum = 2 * N;

	for (int i = 0; i < M; ++ i)
	{
		fin>>x>>y;

		Graf[x].outn.push_back(y);
		Graf[x].out ++;

		Graf[y].inn.push_back(x);
		Graf[y].in ++;		
	}

	do
	{
		nodes.clear();

		for (int i = 1; i <= N; ++ i)
		{
			if (Graf[i].in || Graf[i].out)
				nodes.insert(i);
		}

		if (!nodes.size())
			break;

		Node &node = Graf[*nodes.begin()];

		if (node.in > node.out)
		{
			for (int i = 0; i < node.inn.size(); ++ i)
			{
				Graf[node.inn[i]].out --;
			}

			node.bInFel = false;
			node.in = 0;
		}
		else
		{
			for (int i = 0; i < node.outn.size(); ++ i)
			{
				Graf[node.outn[i]].in --;
			}

			node.bOutFel = false;
			node.out = 0;
		}

		sum --;

	}while(nodes.size());

	fout<<sum<<'\n';

	for (int i = 1; i <= N; ++ i)
	{
		if (Graf[i].bInFel && Graf[i].bOutFel)
			fout<<3<<'\n';
		else if (Graf[i].bInFel)
			fout<<2<<'\n';
		else if (Graf[i].bOutFel)
			fout<<1<<'\n';
		else 
			fout<<0<<'\n';
	}

	fin.close();
	fout.close();

	return 0;
}