Pagini recente » Cod sursa (job #1860977) | Cod sursa (job #347699) | Cod sursa (job #1512140)
#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!