Pagini recente » Cod sursa (job #1319510) | Cod sursa (job #2971013) | Cod sursa (job #143111) | Cod sursa (job #1705699) | Cod sursa (job #1890329)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("zvon.in");
ofstream out("zvon.out");
int n, subs[100005], sol;
vector<int> graph[100005];
void citire();
void get_subs(int x);
void zvon(int x, int step);
int main() {
int t;
in >> t;
for (int test = 1; test <= t; ++test) {
citire();
get_subs(1);
for (int p = 1; p <= n; ++p) {
for (int i = 0; i < graph[p].size(); ++i) {
for (int j = i + 1; j < graph[p].size(); ++j) {
if (subs[graph[p][i]] < subs[graph[p][j]]) swap(graph[p][i], graph[p][j]);
}
}
}
zvon(1, 0);
out << sol << '\n';
}
in.close();
out.close();
return 0;
}
void zvon(int x, int step) {
sol = max(sol, step);
for (int i = 0; i < graph[x].size(); ++i) {
int y = graph[x][i];
step++;
zvon(y, step);
}
}
void get_subs(int x) {
subs[x] = 1;
for (int i = 0; i < graph[x].size(); ++i) {
int y = graph[x][i];
if (subs[y] == 0) get_subs(y);
subs[x] += subs[y];
}
}
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);
}
sol = 0;
}