Cod sursa(job #568566)

Utilizator cdascaluDascalu Cristian cdascalu Data 31 martie 2011 13:59:46
Problema Felinare Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include<fstream>
#include<vector>
#include<bitset>
#define MAX 8192
using namespace std;
int N,M,GI[MAX],GO[MAX],maxim,maxP;
vector<int> G[MAX];
bitset<MAX> in,out,viz,out_n,in_n;
bitset<MAX> GD[MAX];
void detmax()
{
	maxim = 0;
	int i;
	for(i=1;i<=N;++i)
	{
		if(!viz[maxP] && GI[i]+GO[i] > maxim)
		{maxim = abs(GI[i]+GO[i]);maxP = i;}
	}
}
void light(int dir, int x)
{
	for(vector<int>::iterator it = G[x].begin();it!=G[x].end();++it)
	{
		if(GD[x][*it] == 1)
		{
			if(!dir)
			{
				out_n[*it] = 1;
				GO[*it] = 0;
			}
		}
		else
		{
			if(dir)
			{
				in_n[*it] = 1;
				GI[*it] = 0;
			}
		}
	}	
}
int main()
{
	ifstream f("felinare.in");
	f>>N>>M;
	int x,y,i;
	for(i=1;i<=M;++i)
	{
		f>>x>>y;
		GD[x][y] = 1;
		G[x].push_back(y);
		G[y].push_back(x);
		++GO[x];
		++GI[y];
	}
	f.close();
	for(i=1;i<=N;++i)
	{
		if(!GO[i])out[i] = 1;
		if(!GI[i])in[i] = 1;
	}
	detmax();
	while(maxim)
	{
		for(vector<int>::iterator it = G[x].begin();it!=G[x].end();++it)
		{
			if(GD[maxP][*it])
			{
				in[*it] = 1;
				GI[*it] = 0;
				light(0,*it);
			}
			else 
			{
				out[*it] = 1;
				GO[*it] = 0;
				light(1,*it);
			}
		}
		light(0,maxP);
		light(1,maxP);
		viz[maxP] = 1;
		
		maxim = 0;
		detmax();
	}
	int cnt=0;
	for(i=1;i<=N;++i)
		cnt+= (in[i]>0) + (out[i]>0);
	ofstream g("felinare.out");
	g<<cnt<<"\n";
	for(i=1;i<=N;++i)
	{
		if(in[i] && out[i])g<<"3\n";
		if(in[i] && !out[i])g<<"2\n";
		if(!in[i] && out[i])g<<"1\n";
		if(!in[i] && !out[i])g<<"0\n";
	}
	g.close();
	return 0;
}