Cod sursa(job #554064)

Utilizator rares192Preda Rares Mihai rares192 Data 14 martie 2011 16:10:42
Problema Cerere Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<fstream>
using namespace std;

void read();
void solve();

int n;
int a[20][100002];
int rez[100002];
int niv;
int x, y;

int main()
{
	read();
	solve();
	return 0;
}

void read()
{
	ifstream fin("cerere.in");
	fin >> n;
	
	for(int i = 1; i <= n; ++i)
		fin >> rez[i];
	
	while( fin >> x >> y )
		a[0][y] = x;
	
	fin.close();
}

void solve()
{
	ofstream fout("cerere.out");
	
	int q = 1;
	for(niv = 1; q; ++niv)
	{
		q = 0;
		for(int i = 1; i <= n; ++i)
		{
			a[niv][i] = a[niv-1][ a[niv-1][i] ];
			if( a[niv][i] )
				q = 1;
		}
	}
	
	for(int i = 1; i <= n; ++i)
	{
		if( rez[i] == 0)
			fout << 0 << " ";
		else
		{
			x = i;
			int nr1 = 0;
			while( rez[x] != 0)
			{
				niv = 1;
				++nr1;
				y = rez[x];
				while( y )
				{
					if( y % 2 == 1)
					x = a[niv-1][x];
					++niv;
					y = y / 2;
				}
			}
			fout << nr1 << " ";
		}
			
	}
}