Cod sursa(job #3250072)

Utilizator IleaIlea Bogdan Ilea Data 19 octombrie 2024 10:18:23
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

string file="darb";
ifstream fin(file+".in");
ofstream fout(file+".out");

set<int> g[100001];
vector<int> b;
int maxim=0, dmax=0;
void dfs(int i, int d)
{
    b[i]=1;
    if (dmax<d){
        dmax=d;
        maxim=i;
    }
    //cout<<i<<" "<<g[i].size();
    for (auto j:g[i]){
        if (!b[j])dfs(j, d+1);
    }
}
int main()
{
    int n;
    fin>>n;
    b.resize(n+1);
    for (int i=1; i<n; i++){
        int x, y;
        fin>>x>>y;
        g[x].insert(y);
        g[y].insert(x);
        //cout<<x<<" "<<y<<endl;
    }
    dfs(1, 0);
    fill(b.begin(), b.end(), 0);
    dmax=0;
    dfs(maxim, 1);
    fout<<dmax;
    return 0;
}