Pagini recente » Cod sursa (job #100697) | Cod sursa (job #2901533) | Cod sursa (job #511081) | Cod sursa (job #1853541) | Cod sursa (job #1289470)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
typedef std::vector<int>::iterator iter;
std::ifstream f("darb.in");
std::ofstream g("darb.out");
const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
std::vector<int> G[MAXN]; int n;
int dist[MAXN];
std::queue<int> q;
void bfs(int nod)
{
memset(dist, 0x3f, sizeof(dist));
q.push(nod);
dist[nod] = 0;
while (!q.empty()) {
int nd = q.front();
q.pop();
for (iter it = G[nd].begin(); it != G[nd].end(); it++) {
if (dist[*it] > dist[nd] + 1) {
dist[*it] = dist[nd] + 1;
q.push(*it);
}
}
}
}
int main()
{
f >> n;
for (int i = 1, x, y; i <= n; i++) {
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
bfs(1);
int nd = 1, mx = 0;
for (int i = 1; i <= n; i++) {
if (dist[i] >= mx) { mx = dist[i]; nd = i; }
}
bfs(nd);
mx = 0;
for (int i = 1; i <= n; i++) {
mx = std::max(mx, dist[i]);
}
g << mx + 1 << std::endl;
f.close();
g.close();
return 0;
}