Pagini recente » Cod sursa (job #1098956) | Cod sursa (job #1295300) | Cod sursa (job #1140303) | Cod sursa (job #1409305) | Cod sursa (job #1243681)
#include <stdio.h>
#include<string.h>
#define NMAX 100000
using namespace std;
int n,use[NMAX],c[NMAX],D[NMAX],nr,mx,i;
struct lista
{
int nod;
lista *urm;
}*g[NMAX];
void adauga (int i,int j)
{
lista *aux=new lista;
aux->nod=i;
aux->urm=g[j];
g[j]=aux;
}
void bf(int start)
{
lista *p;
memset(use,0,sizeof(use));
use[start]=1;
int nod,st,dr;
st=dr=1;
c[st]=start;
for(D[start]=0;st<=dr;st++)
{
nod=c[st];
for(p=g[nod];p;p=p->urm)
if(!use[p->nod])
{
use[c[++dr]=p->nod]=1;
D[p->nod]=D[nod]+1;
if(D[p->nod]>mx){mx=D[p->nod];nr=p->nod;}
}
}
}
FILE *f1=fopen("darb.in","r"),*f2=fopen("darb.out","w");
int m;
int main()
{
fscanf(f1,"%d",&n);
while(!feof(f1))
{int x,y;fscanf(f1,"%d%d",&x,&y);adauga(x,y);adauga(y,x);}
bf(1);
bf(nr);
fprintf(f2,"%d",mx+1);
fclose(f1);
fclose(f2);
return 0;
}
//Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time.