Cod sursa(job #3123497)

Utilizator MAlex2019Melintioi George Alexandru MAlex2019 Data 24 aprilie 2023 09:44:22
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int maxn = 1e5;
vector<int> adj[maxn + 1];
bool visited[maxn + 1];

int maxdepth, maxnode;

void dfs(int node, int depth) {
    if (depth > maxdepth) {
        maxdepth = depth;
        maxnode = node;
    }
    visited[node] = true;
    int ans = 0;
    for (int child: adj[node])
        if (!visited[child])
            dfs(child, depth + 1);
}

int main() {
    int n;
    fin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        fin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, 1);
    for (int i = 1; i <= n; i++)
        visited[i] = false;
    maxdepth = 0;
    dfs(maxnode, 1);
    fout << maxdepth << '\n';
    
    return 0;
}