Cod sursa(job #2673155)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 15 noiembrie 2020 22:39:09
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>

const int nmax = 1e5 + 5;

int h[nmax];
std::vector<int>l[nmax];

void dfs(int node, int father){
	h[node] = h[father] + 1;
	for (int x : l[node])
		if (x != father) dfs(x, node);
}

int main() {
	std::ifstream fin("darb.in");
	std::ofstream fout("darb.out");
	int n, u, v;
	fin >> n;
	for (int i = 1; i < n; i++) {
		fin >> u >> v;
		l[u].push_back(v);
		l[v].push_back(u);
	}
	dfs(1, 0);
	int mx = 0, nod;
	for (int i = 1; i <= n; i++)
		if (h[i] > mx) mx = h[i], nod = i;
	h[nod] = 1, mx = 0;
	dfs(nod, 0);
	for (int i = 1; i <= n; i++)
		if (h[i] > mx) mx = h[i];
	fout << mx;
}