Pagini recente » Cod sursa (job #2307229) | Cod sursa (job #1835140) | Cod sursa (job #1608688) | Cod sursa (job #2610303) | Cod sursa (job #1164246)
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#define maxN 100001
#define M -2000000000
using namespace std;
FILE *f=fopen("darb.in","r");
FILE *g=fopen("darb.out","w");
queue<int>q;
vector<int>L[maxN];
bool viz[maxN];
int n,maxx,max1=M,max2=M,niv[maxN];
void bfs(int i)
{
memset(viz,false,sizeof(viz));
memset(niv,0,sizeof(niv));
viz[1]=1;
int xcur,j;
q.push(i);
viz[i]=true;
niv[i]=1;
while(!q.empty())
{
xcur=q.front();
q.pop();
for(j=0;j<L[xcur].size();j++)
if(viz[L[xcur][j]]==false)
{
viz[L[xcur][j]]=true;
niv[L[xcur][j]]=niv[xcur]+1;
maxx=niv[L[xcur][j]];
q.push(L[xcur][j]);
}
}
}
int main()
{
int i,x,y,j;
fscanf(f,"%d",&n);
for(i=1;i<n;i++)
{
fscanf(f,"%d%d",&x,&y);
L[x].push_back(y);
L[y].push_back(x);
}
viz[1]=1;
for(j=0;j<L[1].size();j++)
{
bfs(L[1][j]);
if(maxx>=max1)
{
max2=max1;
max1=maxx;
}
else if(maxx>max2)max2=maxx;
}
if(max1==M)max1=-1;
fprintf(g,"%d",max1+max2+1);
return 0;
}