Cod sursa(job #2096408)

Utilizator andr3i_kaabAndrei Ciineanu andr3i_kaab Data 29 decembrie 2017 03:38:44
Problema Diametrul unui arbore Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
#define M 100005

using namespace std;

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

vector <int> a[M];
int n, m, s, k, viz[M];
queue <int> q;

void bfs(int s)
{
    memset(viz, -1, sizeof(viz));

    q.push(s); viz[s]=0;
    while ( !q.empty() )
    {
        k=q.front();
        q.pop();
        for (int i=0; i<a[k].size(); i++)
          if ( viz[ a[k][i] ]== -1 )
          {
              viz[ a[k][i] ] = viz[k]+1;
              q.push( a[k][i] );
          }
    }
}
int main()
{
    int i, j, x, y;

    f>>n;

    for (i=1; i<=n-1; i++)
    {
        f>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    int max1=-1;
    for (i=1; i<=n; i++)
      {
          bfs(i);
          for (j=1; j<=n; j++) if (max1<viz[j]) max1=viz[j];
      }
    g<<max1+1;
    return 0;
}