Cod sursa(job #2719243)

Utilizator AlexZeuVasile Alexandru AlexZeu Data 9 martie 2021 18:32:26
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;

vector<int> edges[100010];
bool visited[100010];
int n, ans, lowest_point;

ifstream fin("darb.in");
ofstream fout("darb.out");

void dfs(int x, int distanta) {
    if (visited[x] == false) {
        visited[x] = true;
        if (distanta > ans) {
            ans = distanta;
            lowest_point = x;
        }
        for (int i = 0; i < edges[x].size(); ++i) {
            dfs(edges[x][i], distanta + 1);
        }
    }
}

int main() {
    fin.tie(0);
    ios::sync_with_stdio(0);
    fin >> n;
    for (int i = 1; i <= n - 1; ++i) {
        int x, y;
        fin >> x >> y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    dfs(1, 0);
    for (int i = 1; i <= n; ++i) {
        visited[i] = false;
    }
    ans = 0;
    dfs(lowest_point, 0);
    fout << ans + 1;
    return 0;
}