Pagini recente » Cod sursa (job #3121061) | Cod sursa (job #3239028) | Cod sursa (job #1351223) | Cod sursa (job #803273) | Cod sursa (job #1098622)
#include <fstream>
#include <vector>
#define Nmax 100100
#define Vecin Graph[Nod][i]
using namespace std;
vector <int> Graph[Nmax];
int N, Solution, MiddleMan, Dist[Nmax];
void Dfs(int Nod, int Tata) {
Dist[Nod] = Dist[Tata] + 1;
if(Dist[Nod] > Dist[MiddleMan])
MiddleMan = Nod;
for(int i = 0; i < Graph[Nod].size(); i++)
if(Vecin != Tata)
Dfs(Vecin, Nod);
}
void Solve() {
Dist[1] = 1;
Dfs(1, 0);
Dist[MiddleMan] = 1;
Dfs(MiddleMan, 0);
Solution = Dist[MiddleMan];
}
void Read() {
int i, x, y;
ifstream in("darb.in");
in >> N;
for(i = 1; i < N; i++) {
in >> x >> y;
Graph[x].push_back(y);
Graph[y].push_back(x);
}
in.close();
}
void Write() {
ofstream out("darb.out");
out << Solution << '\n';
out.close();
}
int main() {
Read();
Solve();
Write();
return 0;
}