Cod sursa(job #3233376)

Utilizator DobraVictorDobra Victor Ioan DobraVictor Data 3 iunie 2024 10:15:10
Problema Zvon Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <vector>
#include <algorithm>

const int32_t MAX_N = 100000;

std::vector<int32_t> adj[MAX_N];
int32_t dp[MAX_N];
int32_t dpSort[MAX_N];

int32_t max(int32_t x, int32_t y) {
	return (x > y) ? x : y;
}
void DFS(int32_t node) {
	int32_t top = 0;
	for(int32_t next : adj[node]) {
		DFS(next);
		dpSort[top++] = dp[next] + 1;
	}
	std::sort(dpSort, dpSort + top, std::greater<int32_t>());

	for(int32_t i = 0; i != top; ++i)
		dp[node] = max(dp[node], dpSort[i] + i);
}

int main() {
	std::ifstream fin("zvon.in");
	std::ofstream fout("zvon.out");

	int32_t t;
	fin >> t;
	while(t--) {
		int32_t n;
		fin >> n;
		for(int32_t i = 0; i != n - 1; ++i) {
			int32_t x, y;
			fin >> x >> y;
			--x; --y;
			adj[x].push_back(y);
		}
		DFS(0);
		fout << dp[0] << '\n';

		for(int32_t i = 0; i != n; ++i)
			adj[i].clear();
		for(int32_t i = 0; i != n; ++i)
			dp[i] = 0;
	}

	fin.close();
	fout.close();

	return 0;
}