Pagini recente » Cod sursa (job #1200073) | Cod sursa (job #1784631) | Cod sursa (job #2568278) | Cod sursa (job #2808632) | Cod sursa (job #2254594)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
constexpr const int NMAX = 1e5 + 5;
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector < int > g[NMAX];
bool viz[NMAX];
int dist[NMAX];
int n;
pair <int, int> bfs(int nod){
viz[0] = !viz[0];
queue < int > q;
q.push(nod);
dist[nod] = 1;
viz[nod] = viz[0];
while(!q.empty()){
nod = q.front();
q.pop();
for(int i : g[nod])
if(viz[i] != viz[0]){
q.push(i);
viz[i] = viz[0];
dist[i] = dist[nod] + 1;
}
}
return {nod, dist[nod]};
}
int main(){
fin >> n;
for(int i = 1, x, y; i < n; i++){
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
fin.close();
fout << bfs(bfs(1).first).second << '\n';
fout.close();
return 0;
}