Pagini recente » Profil analucaana | Cod sursa (job #561743) | Cod sursa (job #1310496) | Cod sursa (job #1321877) | Cod sursa (job #1467694)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ofstream fout("darb.out");
ifstream fin("darb.in");
const int NMAX = 100005;
int n, last_idx, last;
int Nivel[NMAX];
vector<int> Graf[NMAX];
queue<int> Q;
void bfs(int first)
{
last_idx = 0;
for(int i=1; i<=n-1; i++) Nivel[i] = 0;
Nivel[first] = 1;
Q.push(first);
while(!Q.empty()) {
int nod = Q.front();
Q.pop();
for(auto x : Graf[nod]) {
if(!Nivel[x]) {
Nivel[x] = Nivel[nod] + 1;
Q.push(x);
if(Nivel[x] > last_idx) {
last_idx = Nivel[x];
last = x;
}
}
}
}
}
int main()
{
fin >> n;
for(int i=1, x, y; i<=n-1; i++) {
fin >> x >> y;
Graf[x].push_back(y);
Graf[y].push_back(x);
}
bfs(1);
bfs(last);
fout << last_idx << '\n';
return 0;
}