Pagini recente » Cod sursa (job #3042024) | Cod sursa (job #982284) | Cod sursa (job #1539866) | Cod sursa (job #402684) | Cod sursa (job #2279638)
#include<stdio.h>
#include <string.h>
#include<vector>
using namespace std;
#define MAXN 100000
int M,N;
vector<int> L[MAXN];
unsigned char visited[MAXN]; // vector pentru marcarea nodurilor
int lastNode;
int maxLength;
void depth(int nod,int length){
// procesarea nodului de indice nod
visited[nod]=1;
if(length>maxLength){
maxLength=length;
lastNode=nod;
}
vector<int>::iterator it;
for (it = L[nod].begin(); it != L[nod].end(); ++it) {
if(visited[*it]==0)
depth(*it,length+1);
}
}
int main(){
freopen("darb.in","rt",stdin);
freopen("darb.out","wt",stdout);
scanf("%d",&N);
int x,y;
int nb=0;
for(int i=0;i<(N-1);i++){
scanf("%d %d",&x,&y);
L[x-1].push_back(y-1);
L[y-1].push_back(x-1);
}
depth(0,1);
memset(visited,0,N);
maxLength=0;
depth(lastNode,1);
printf("%d\n",maxLength);
return 0;
}