Cod sursa(job #2916604)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 30 iulie 2022 19:44:37
Problema Diametrul unui arbore Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
/**
 *    author:  R0L3eX
 *    created: 30.07.2022 19:32:03
 *    quote: Claustrophobic? Who would ever be afraid of Santa Clause?
**/

#include "bits/stdc++.h"

using namespace std;

#if defined(ONPC)
#include "bits/debug.h"
#endif

#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1e9 + 7;
const int mxN = 2e5;
const int INF = INT_MAX;
const char nl = '\n';

vector<vector<int>> adj;

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

int main() {
  cin.tie(0)->sync_with_stdio(0);

  int n;
  fin >> n;

  adj = vector<vector<int>>(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    fin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  array<int, 3> last;
  queue<array<int, 3>> Q;
  //  node from depth
  Q.push({0, 0, 0});
  while (!Q.empty()) {
    array<int, 3> P = Q.front();
    last = P;
    Q.pop();
    for (int u : adj[P[0]]) {
      if (u == P[1]) continue;
      Q.push({u, P[0], P[2] + 1});
    }
  }

  Q.push({last[0], last[0], 0});
  while (!Q.empty()) {
    array<int, 3> P = Q.front();
    last = P;
    Q.pop();
    for (int u : adj[P[0]]) {
      if (u == P[1]) continue;
      Q.push({u, P[0], P[2] + 1});
    }
  }

  fout << last[2] + 1 << nl;
}