Cod sursa(job #2629479)

Utilizator Ionut_neuer58Raducu Ioan Stefan Ionut_neuer58 Data 20 iunie 2020 23:42:01
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

vector <int> v[100001];
bool viz[100001];
int n, yy, xx, nodmax, maxdepth, depth;

void readit()
{
    in>>n;
    for(int i=1; i<n; i++) in>>yy>>xx, v[yy].push_back(xx), v[xx].push_back(yy);
}

void DFS(int nod)
{
    viz[nod]=1;
    ++depth;
    for(int i=0; i<v[nod].size(); i++)
        if(!viz[v[nod][i]]) DFS(v[nod][i]);
    if(depth>maxdepth) maxdepth=depth, nodmax=nod;
    --depth;
}

void clearvec()
{
    for(int i=1; i<=n; i++) viz[i]=0;
}

int main()
{
    readit();
    DFS(1);
    clearvec();
    DFS(nodmax);
    out<<maxdepth;
    return 0;
}