Cod sursa(job #1808701)

Utilizator xSliveSergiu xSlive Data 17 noiembrie 2016 23:33:42
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <vector>
#include <string.h>
#include <fstream>
#define pb push_back
#define NMAX 200010
using namespace std;

vector<int> v[NMAX];
vector<int> visited(NMAX, 0);
int nod_max = -1;
int nod_max_grade = 0;

void dfs(int x,int deep) {
	visited[x] = 1;
	if (deep > nod_max_grade) {
		nod_max_grade = deep;
		nod_max = x;
	}
	for (auto nod : v[x]) {
		if (!visited[nod]) {
			dfs(nod, deep + 1);
		}
	}
	visited[x] = 0;
}

int main() {
	int n,el1,el2;
	ifstream f("darb.in");
	ofstream g("darb.out");
	f >> n;
	for (int i = 0; i < n - 1; i++) {
		f >> el1 >> el2;
		v[el1].pb(el2);
		v[el2].pb(el1);
	}
	dfs(1, 1);
	dfs(nod_max,1);
	g << nod_max_grade;
	return 0;
}