Cod sursa(job #3293745)

Utilizator mihai21681Maricutu Mihai Alexandru mihai21681 Data 12 aprilie 2025 15:00:38
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
// darb.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream cin("darb.in");
ofstream cout("darb.out");
int n, x, y, nivel[100001], ma, P;
vector<int> G[100001];
bool v[100001];
void dfs(int x, int h) {
	if (!v[x]) {
		nivel[x] = h;
		v[x] = 1;
		if (h > ma) {
			P = x; ma = h;
		}
		for (int i : G[x])
			dfs(i, h + 1);
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	dfs(1, 0);
	fill(v + 1, v + 1 + n, 0);
	fill(nivel + 1, nivel + 1 + n, 0);
	ma = 0;
	dfs(P, 0);
	cout << ma + 1;
}