Problem p02371 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

pair<int, long long>
getFarthestNode(int from, vector<bool> &visited,
                const vector<vector<pair<int, int>>> &tree) {
  visited[from] = true;

  int farthestNode = from;
  long long farthestDistance = 0;

  for (pair<int, int> p : tree[from]) {
    int to = p.first;
    int w = p.second;
    if (visited[to]) {
      continue;
    }

    pair<int, long long> n_l = getFarthestNode(to, visited, tree);

    // node -> to -> n_l.firstの距離
    long long distance = w + n_l.second;

    if (distance > farthestDistance) {
      farthestNode = n_l.first;
      farthestDistance = distance;
    }
  }

  return {farthestNode, farthestDistance};
}

long long tree_diameter(unsigned int num_of_node,
                        const vector<vector<pair<int, int>>> &tree) {
  vector<bool> visited(num_of_node, false);
  pair<int, long long> p1 = getFarthestNode(0, visited, tree);

  visited = vector<bool>(num_of_node, false);
  pair<int, long long> p2 = getFarthestNode(p1.first, visited, tree);

  return p2.second;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<vector<pair<int, int>>> tree(n);

  FOR(i, 0, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    tree[s].emplace_back(make_pair(t, w));
    tree[t].emplace_back(make_pair(s, w));
  }

  cout << tree_diameter(n, tree) << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

pair<int, long long> getFarthestNode(int node, vector<bool> &visited,
                                     const vector<vector<int>> &tree) {
  visited[node] = true;

  int farthestNode = node;
  long long farthestDistance = 0;

  FOR(to, 0, tree.size()) {
    if (visited[to]) {
      continue;
    }
    if (tree[node][to] == INT_MAX) {
      continue;
    }

    pair<int, long long> n_l = getFarthestNode(to, visited, tree);

    // node -> to -> n_l.firstの距離
    long long distance = tree[node][to] + n_l.second;

    if (distance > farthestDistance) {
      farthestNode = n_l.first;
      farthestDistance = distance;
    }
  }

  return {farthestNode, farthestDistance};
}

long long tree_diameter(unsigned int num_of_node,
                        const vector<vector<int>> &tree) {
  vector<bool> visited(num_of_node, false);
  pair<int, long long> p1 = getFarthestNode(0, visited, tree);

  visited = vector<bool>(num_of_node, false);
  pair<int, long long> p2 = getFarthestNode(p1.first, visited, tree);

  return p2.second;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<vector<int>> tree(n, vector<int>(n, INT_MAX));

  FOR(i, 0, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    tree[s][t] = w;
    tree[t][s] = w;
  }

  cout << tree_diameter(n, tree) << endl;

  return 0;
}