Cod sursa(job #3255259)

Utilizator MateiAlex24Diamandi Matei MateiAlex24 Data 9 noiembrie 2024 20:59:38
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int> noduri[100001];
int n, dmax=0, ultim_nod, ultim_dist;
bool vizitat[100001];

void dfs(int nod, int dist){
    //cout<<dmax<<endl;
    if (dist > dmax){
        dmax = dist;
        ultim_nod = nod;
        ultim_dist = dist;
    }
    vizitat[nod] = true;
    for (auto vecin: noduri[nod]){
        if (!vizitat[vecin])
            dfs(vecin, dist+1);
    }

    vizitat[nod] = false;
}

int main()
{
    fin>>n;
    for (int i=0; i<n-1; i++){
        int x,y;
        fin>>x>>y;
        noduri[x].push_back(y);
        noduri[y].push_back(x);
    }
    dfs(1,0);
    //cout<<ultim_nod<<" "<<ultim_dist<<endl;
    dfs(ultim_nod, 0);
    //cout<<ultim_nod<<" "<<ultim_dist;
    fout<<ultim_dist+1;
	return 0;
}