Pagini recente » Cod sursa (job #53736) | Cod sursa (job #2975402) | Cod sursa (job #2546643) | Cod sursa (job #55779) | Cod sursa (job #551211)
Cod sursa(job #551211)
// http://infoarena.ro/problema/zvon
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
#define maxSize 100001
ifstream in("zvon.in");
ofstream out("zvon.out");
vector<int> graph[maxSize];
void read();
int depthFirst(int node);
int main() {
int tests;
in >> tests;
for(int i=1;i<=tests;i++) {
read();
out << depthFirst(1) << "\n";
}
in.close();
out.close();
return (0);
}
void read() {
int nodes,father,son;
in >> nodes;
for(int i=1;i<nodes;i++)
graph[i].clear();
for(int i=1;i<nodes;i++) {
in >> father >> son;
graph[father].push_back(son);
}
}
int depthFirst(int node) {
vector<int> dist;
vector<int>::iterator it,end;
end = graph[node].end();
for(it=graph[node].begin();it!=end;++it)
dist.push_back(depthFirst(*it));
sort(dist.begin(),dist.end());
int stop = dist.size();
int maxim = 0;
for(int i=0;i<stop;i++)
maxim = max(maxim,dist[i]+stop-i);
return maxim;
}