Cod sursa(job #2629126)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 19 iunie 2020 09:34:29
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n,distmax,nodmax;
vector<int> muchii[100001];
bool use[100001];
void dfs(int nod,int dist)
{
    use[nod]=1;
    if(dist>distmax)
    {
        nodmax=nod;
        distmax=dist;
    }
    for(auto i:muchii[nod])
        if(!use[i])
            dfs(i,dist+1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);
    fin>>n;
    for(int i=1;i<n;i++)
    {
        int a,b;
        fin>>a>>b;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
    }
    dfs(1,1);
    memset(use,0,sizeof(use));
    distmax=0;
    dfs(nodmax,1);
    fout<<distmax;
    return 0;
}