Pagini recente » Cod sursa (job #2502769) | Cod sursa (job #225167) | Cod sursa (job #1626051) | Cod sursa (job #3139109) | Cod sursa (job #2229184)
#include <bits/stdc++.h>
using namespace std;
vector <int> v[100005];
deque <int> Q;
int n, x, i, y, ct, poz1, a[100005], poz2;
bool viz[100005];
void BFS(int x, int & ct, int & poz)
{
Q.push_front(x);
viz[x] = true;
a[x] = 1;
while(!Q.empty())
{
int k = Q.front();
ct = a[k];
poz = k;
for(int i = 0;i < v[k].size();i++)
if(viz[v[k][i]] == false)
{
viz[v[k][i]] = true;
a[v[k][i]] = a[k] + 1;
Q.push_back(v[k][i]);
}
Q.pop_front();
}
memset(viz, 0, sizeof(viz));
}
int main()
{
ifstream f("darb.in");
ofstream g("darb.out");
f >> n;
for(i = 1;i <= n - 1;i++)
{
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
ct = 0;
BFS(1, ct, poz1);
ct = 1;
BFS(poz1, ct, poz2);
g << ct;
return 0;
}