Pagini recente » Cod sursa (job #1343924) | Cod sursa (job #3201249) | Cod sursa (job #1237580) | Cod sursa (job #3200614) | Cod sursa (job #2406313)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
#define nmax 100001
int n, x, y, d[nmax];
bool u[nmax];
queue<int> q;
vector<int> v[nmax];
void bfs(int nod) {
q.pop();
u[nod] = true;
for (int i = 0; i < v[nod].size(); ++i) if (!u[v[nod][i]]) { q.push(v[nod][i]); d[v[nod][i]] = d[nod] + 1; }
x = nod;
}
int main()
{
f >> n;
for (int i = 1; i < n; ++i) {
f >> x >> y;
v[x].push_back(y), v[y].push_back(x);
}
q.push(1);
while (!q.empty()) bfs(q.front());
q.push(x);
for (int i = 1; i <= n; ++i) { u[i] = false; d[i] = 0; }
while (!q.empty()) bfs(q.front());
g << d[x] + 1 << '\n';
return 0;
}