Cod sursa(job #1206410)

Utilizator mucalmicmarcel almic mucalmic Data 9 iulie 2014 20:52:20
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <set>

#define N 100001

#define max(a,b) ((a) >= (b) ? (a) : (b))

using namespace std;

int viz[N] = {0}, nv, dst = 0;
vector <int> v[N];

void dfs(int x, int d) {
	int y, z;
	viz[x] = 1;
	
	if (d > dst) {
		dst = d;
		nv = x;
	}
	
	
	for (z = 0; z < v[x].size(); z++) {
		y = v[x][z];
		if (!viz[y]) {
			dfs(y, d+1);
		}
	}
}


int main () {

	FILE *fi, *g;
	fi = freopen("darb.in", "r", stdin);
	g = freopen("darb.out", "w", stdout);

	int i, j, k, n, x, y, d;
	char c;
	
	scanf("%d", &n);
	
	for (j = 1; j<n; j++) {
	
		scanf("%d %d", &x, &y);
		v[y].push_back(x);
		v[x].push_back(y);
	}
	
	memset (viz, 0, sizeof(viz));
		
	dst = nv = 0;	
	dfs(1, 1);
	//printf("%d\n", nv);
	memset (viz, 0, sizeof(viz));
	dst = 0;
	dfs(nv, 1);
	//printf("%d\n", nv);
	
	printf("%d\n", dst);
		
	
	return 0;
	
}