Pagini recente » Borderou de evaluare (job #59264) | Cod sursa (job #2133851)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define NMAX 100005
int n, maxi, far;
vector<int> a[NMAX];
bool viz[NMAX];
void DFS(int nod, int cost){
viz[nod] = true;
if(cost > maxi){
far = nod;
maxi = cost;
}
int i;
for(i=0; i<a[nod].size(); i++){
if(!viz[ a[nod][i] ])
DFS(a[nod][i], cost + 1);
}
}
int main(){
FILE *fin, *fout;
fin = fopen("darb.in", "r");
fout = fopen("darb.out", "w");
int i, x, y;
fscanf(fin, "%d", &n);
for(i=1; i<=n - 1; i++){
fscanf(fin, "%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
DFS(1, 1);
memset(viz, false, sizeof(viz));
DFS(far, 1);
fprintf(fout, "%d", maxi);
fclose(fin);
fclose(fout);
return 0;
}