Pagini recente » Cod sursa (job #1150882) | Cod sursa (job #3158254) | Cod sursa (job #682997) | Cod sursa (job #561077) | Cod sursa (job #1890393)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("zvon.in");
ofstream out("zvon.out");
int n, subs[100005];
vector<int> graph[100005];
void citire();
void zvon(int x);
int main() {
int t;
in >> t;
for (int test = 1; test <= t; ++test) {
citire();
zvon(1);
out << subs[1] << '\n';
}
in.close();
out.close();
return 0;
}
void zvon(int x) {
int maxi = 0;
int freq = 0;
for (int i = 0; i < graph[x].size(); ++i) {
int y = graph[x][i];
if (subs[y] == 0) {
zvon(y);
}
if (subs[y] > maxi) {
maxi = subs[y];
freq = 1;
} else if (subs[y] == maxi) {
freq++;
}
}
subs[x] = maxi + 1 + (freq - 1);
}
void citire() {
in >> n;
for (int i = 1; i <= n; ++i) {
graph[i].clear();
subs[i] = 0;
}
for (int i = 1; i < n; ++i) {
int x, y;
in >> x >> y;
graph[x].push_back(y);
}
}