Pagini recente » Cod sursa (job #823199) | Cod sursa (job #537150) | Cod sursa (job #1975726) | Cod sursa (job #647912) | Cod sursa (job #3258075)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("darb.in");
ofstream cout("darb.out");
int n, x, y;
vector<vector<int>> graf;
vector<int> d;
void bfs(int node)
{
queue<int> q;
d[node] = 1;
q.push(node);
while(!q.empty())
{
node = q.front();
q.pop();
for(auto next:graf[node])
if(!d[next])
{
d[next] = d[node] + 1;
q.push(next);
}
}
}
int main()
{
cin >> n;
graf.assign(n+1, vector<int>());
d.resize(n+1);
for(int i=1; i<n; i++)
{
cin >> x >> y;
graf[x].push_back(y);
graf[y].push_back(x);
}
bfs(1);
int maxi = -1, node, s=0;
for(int i=1; i<=n; i++)
if(maxi < d[i])
{
maxi = d[i];
node = i;
}
s += maxi;
maxi = -1;
fill(d.begin(), d.end(), 0);
bfs(node);
for(int i=1; i<=n; i++)
maxi = max(maxi, d[i]);
s = maxi;
cout << s;
return 0;
}