Pagini recente » Cod sursa (job #1165047) | Istoria paginii utilizator/azinganga_ale | Cod sursa (job #2741056) | Cod sursa (job #2487347) | Cod sursa (job #1474585)
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAX 100005
using namespace std;
int n, i, x, y, distmax, nodemax;
vector<int> l[MAX];
bool viz[MAX];
void dfs(int node, int dist);
int main(){
freopen("darb.in", "r", stdin);
freopen("darb.out", "w", stdout);
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%d%d", &x, &y);
l[x].push_back(y);
l[y].push_back(x);
}
dfs(1, 1);
memset(viz, 0, n + 1);
dfs(nodemax, 1);
printf("%d\n", distmax);
return 0;
}
void dfs(int node, int dist){
viz[node] = 1;
if(dist > distmax){
distmax = dist;
nodemax = node;
}
for(int i = 0; i < l[node].size(); i++)
if(!viz[l[node][i]])
dfs(l[node][i], dist + 1);
}