Pagini recente » Cod sursa (job #528678) | Cod sursa (job #2801263) | Cod sursa (job #306176) | Diferente pentru problema/marfa2 intre reviziile 3 si 4 | Cod sursa (job #2780705)
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int lim=1e5+4;
queue<pair<int,int> > q;
vector<int> vec[lim];
bool ok[lim];
int n,x,y,d;
int main()
{
in>>n;
for(int i=1;i<n;++i)
in>>x>>y,
vec[x].push_back(y),
vec[y].push_back(x);
int cap,dist;
q.push({1,0});
ok[1]=true;
while(!q.empty())
{
int nod=q.front().first;
int d=q.front().second; q.pop();
cap=nod, dist=d;
for(int x:vec[nod])
if(!ok[x]) ok[x]=true,
q.push({x,d+1});
}
memset(ok,0,sizeof(ok));
q.push({cap,0});
ok[cap]=true;
while(!q.empty())
{
int nod=q.front().first;
int d=q.front().second; q.pop();
cap=nod, dist=d;
for(int x:vec[nod])
if(!ok[x]) ok[x]=true,
q.push({x,d+1});
}
out<<dist+1<<'\n';
return 0;
}