Cod sursa(job #1453243)

Utilizator EpictetStamatin Cristian Epictet Data 23 iunie 2015 08:53:28
Problema Zvon Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

#define Nmax 100010
#define sef first
#define bot second

using namespace std;

ifstream fin ("zvon.in");
ofstream fout ("zvon.out");

int T, N, timp, B[Nmax];
bool viz[Nmax];
pair < int, int > A[Nmax];
vector < int > V[Nmax];

bool cmp(pair < int, int > a, pair < int, int > b)
{
	if (a.sef > b.sef) return true;
	else if (a.sef == b.sef && a.bot > b.bot) return true;
	return false;
}

void Reset_Values()
{
	memset(B, 0, sizeof(B));
	memset(viz, false, sizeof(viz));
	for (int i = 1; i <= N; i++) V[i].clear();
}

void DFS(int nod)
{
	viz[nod] = true;
	timp = max (B[nod], timp);
	
	for (vector < int > :: iterator it = V[nod].begin(); it != V[nod].end(); it++)
	{
		if (!viz[*it])
		{
			B[*it] = B[nod] + 1;
			B[nod] ++;
			DFS(*it);
		}
	}
}

int main()
{
	fin >> T;
	while (T--)
	{
		Reset_Values();
		fin >> N;
		N --;
		for (int i = 1; i <= N; i++)
		{
			fin >> A[i].sef >> A[i].bot;
			V[A[i].sef].push_back(A[i].bot);
		}
		
		timp = 0;
		
		DFS(1);
		
		fout << timp << '\n'; 
	}
	
	return 0;
}