Cod sursa(job #2188526)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 27 martie 2018 10:44:13
Problema Zvon Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream f("zvon.in");
ofstream g("zvon.out");
const int NMAX = 100005;
vector <int> sons[NMAX];
int father[NMAX];
int timp[NMAX];
int n, teste, m, mx = 0;
void init()
{
	for (int i = 1; i <= n; i++)
	{
		mx = 0;
		father[i] = 0;
		sons[i].clear();
		timp[i] = 0;
	}
}
void in()
{
	f >> n;
	for (int i = 1; i < n; i++)
	{
		int x, y;
		f >> x >> y;
		father[y] = x;
		sons[x].push_back(y);
	}
}
int get_max(int a, int b)
{
	if (a < b)
		return b;
	return a;
}
void out()
{
	g << mx << "\n";
}
bool cmp(int i, int j)
{
	return sons[i].size() > sons[j].size();
}
void Sort()
{
	for (int i = 1; i <= n; i++)
		sort(sons[i].begin(), sons[i].end(), cmp);
}
void DFS(int nod, int grad)
{
	timp[nod] = timp[father[nod]] + grad;
	mx = get_max(mx, timp[nod]);
	for (int i = 0; i < sons[nod].size(); i++)
		DFS(sons[nod][i], i + 1);
}
int main()
{
	int nr_teste;
	f >> nr_teste;
	while (nr_teste--)
	{
		in();
		Sort();
		DFS(1, 0);
		out();
		init();
	}
}