Pagini recente » Cod sursa (job #2793129) | Cod sursa (job #1699223) | Cod sursa (job #1773251) | Cod sursa (job #2932640) | Cod sursa (job #3286664)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
int n, ans, nodUltim, x, y;
vector <int> a[100005];
queue <int> q;
inline bfs(int start)
{
bool viz[100005] = {0};
int cost[100005] = {0};
cost[start] = 1;
viz[start] = 1;
q.push(start);
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto it : a[nod]) {
if (!viz[it]) {
viz[it] = 1;
q.push(it);
cost[it] = cost[nod] + 1;
ans = cost[it];
nodUltim = it;
}
}
}
}
int main()
{
f >> n;
for (int i = 1; i < n; ++i) {
f >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
bfs(1);
bfs(nodUltim);
g << ans;
return 0;
}