Cod sursa(job #2783889)

Utilizator RobertAcAcatrinei Robert-Marian RobertAc Data 15 octombrie 2021 11:50:33
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
vector<int> vec[nmax];
int maxx=1;
int maxdepth=0;
bool viz[nmax];
int dfs(int depth,int nod){
    viz[nod]=1;
    int maxxx=depth;
    if(depth>maxdepth){maxx=nod;maxdepth=depth;}
    for(auto i:vec[nod]){
        if(!viz[i]){
            maxxx=max(dfs(depth+1,i),maxxx);
        }
    }
    return maxxx;
}
int main(){
    int n;
    in>>n;
    int x,y;
    for(int i=1;i<n;i++){
        in>>x>>y;
        vec[x].push_back(y);
        vec[y].push_back(x);
    }
    dfs(1,maxx);
    memset(viz,0,sizeof(viz));
    out<<dfs(1,maxx);
}