Cod sursa(job #2482630)

Utilizator PatriciaCretoiuCretoiu Patricia PatriciaCretoiu Data 28 octombrie 2019 17:57:17
Problema Diametrul unui arbore Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int N=1e5+2;
int n,i,x,y,m,len,f[N],sol;
vector<int>G[N];

void DFS(int x)
{
    f[x]=1;
    len++;
    sol=x;
    for(i=0; i<G[x].size(); i++)
        if(f[G[x][i]]==0) DFS(G[x][i]);
}

int main()
{
    in>>n;
    for(i=1; i<n; i++)
    {
        in>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1);
    len=0;
    memset(f,0,n*404);
    DFS(sol);
    out<<len;
}