Cod sursa(job #1512140)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 27 octombrie 2015 18:49:45
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>

#define DIM 100005
#define infile "zvon.in"
#define outfile "zvon.out"

using namespace std;

int dp[DIM];

vector<int> graph[DIM];

void dfs(int node) {

	for (int adj : graph[node]) {

		dfs(adj);

	}

	vector<int> aux;

	for (int adj : graph[node]) {

		aux.push_back(dp[adj]);

	}

	sort(aux.begin(), aux.end());

	for (unsigned int index = 0; index < aux.size(); ++index) {

		dp[node] = max(dp[node], aux[index] + (int)(aux.size() - index));

	}

}

int main() {

	ifstream fin(infile);
	ofstream fout(outfile);

	int testCount;

	fin >> testCount;

	for (; testCount; --testCount) {

		int nodeCount;

		fin >> nodeCount;

		for (int index = 1; index <= nodeCount; ++index) {

			graph[index].clear();

		}

		for (int index = 1; index < nodeCount; ++index) {

			int firstNode, secondNode;

			fin >> firstNode >> secondNode;

			graph[firstNode].push_back(secondNode);

		}

		memset(dp, 0, sizeof dp);

		dfs(1);

		fout << dp[1] << '\n';

	}

	return 0;

}

//Trust me, I'm the Doctor!