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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

const int inf = 1e9 + 9;

struct edge {
  int to, cost;
};

int n, r;
vector<edge> G[1550];
vector<int> V[1550];
// vector<int> L;

int d[1550];
bool f[1550], L[1550];

void path(int u) {
  L[u] = true;
  // L.push_back(u);
  if (d[u] == 0)
    return;
  for (int i = 0; i < V[u].size(); i++) {
    path(V[u][i]);
  }
}

int main() {
  cin >> n >> r;
  for (int i = 0; i < r; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    s--;
    t--;
    edge e;
    e.to = t;
    e.cost = d;
    G[s].push_back(e);
    e.to = s;
    G[t].push_back(e);
  }

  priority_queue<P, vector<P>, greater<P>> q;

  int max_d = 0;
  for (int i = 0; i < n; i++) {
    q.push(P(0, i));
    fill(d, d + n, inf);
    d[i] = 0;

    vector<int> E;
    int max_c = 0;

    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int j = 0; j < G[v].size(); j++) {
        edge e = G[v][j];

        if (d[e.to] == d[v] + e.cost) {
          V[e.to].push_back(v);
          q.push(P(d[e.to], e.to));
        }
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          V[e.to].clear();
          V[e.to].push_back(v);
          q.push(P(d[e.to], e.to));
        }
      }
    }

    E.clear();
    for (int j = 0; j < n; j++) {
      if (d[j] == max_c) {
        E.push_back(j);
      }
      if (d[j] > max_c) {
        max_c = d[j];
        E.clear();
        E.push_back(j);
      }
    }

    if (max_c == max_d) {
      for (int j = 0; j < E.size(); j++) {
        path(E[j]);
      }
    }
    if (max_d < max_c) {
      // L.clear();
      memset(L, false, sizeof(L));
      max_d = max_c;
      for (int j = 0; j < E.size(); j++) {
        path(E[j]);
      }
    }
  }

  // sort(L.begin(), L.end());
  // L.erase(unique(L.begin(), L.end()), L.end());
  /*
  for (int i = 0; i < L.size(); i++){
    f[i] = true;
  }
  */

  vector<int> p;
  for (int i = 0; i < n; i++) {
    if (!L[i])
      p.push_back(i + 1);
  }

  cout << p.size() << endl;
  for (int i = 0; i < p.size(); i++) {
    cout << p[i] << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

const int inf = 1e9 + 9;

struct edge {
  int to, cost;
};

int n, r;
vector<edge> G[1550];
vector<int> V[1550];
vector<int> L;

int d[1550];
bool f[1550];

void path(int u) {
  L.push_back(u);
  if (d[u] == 0)
    return;
  for (int i = 0; i < V[u].size(); i++) {
    path(V[u][i]);
  }
}

int main() {
  cin >> n >> r;
  for (int i = 0; i < r; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    s--;
    t--;
    edge e;
    e.to = t;
    e.cost = d;
    G[s].push_back(e);
    e.to = s;
    G[t].push_back(e);
  }

  priority_queue<P, vector<P>, greater<P>> q;

  int max_d = 0;
  for (int i = 0; i < n; i++) {
    q.push(P(0, i));
    fill(d, d + n, inf);
    d[i] = 0;

    vector<int> E;
    int max_c = 0;

    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int j = 0; j < G[v].size(); j++) {
        edge e = G[v][j];

        if (d[e.to] == d[v] + e.cost) {
          V[e.to].push_back(v);
          q.push(P(d[e.to], e.to));
        }
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          V[e.to].clear();
          V[e.to].push_back(v);
          q.push(P(d[e.to], e.to));
        }
      }
    }

    E.clear();
    for (int j = 0; j < n; j++) {
      if (d[j] == max_c) {
        E.push_back(j);
      }
      if (d[j] > max_c) {
        max_c = d[j];
        E.clear();
        E.push_back(j);
      }
    }

    if (max_c == max_d) {
      for (int j = 0; j < E.size(); j++) {
        path(E[j]);
      }
    }
    if (max_d < max_c) {
      L.clear();
      max_d = max_c;
      for (int j = 0; j < E.size(); j++) {
        path(E[j]);
      }
    }
  }

  sort(L.begin(), L.end());
  L.erase(unique(L.begin(), L.end()), L.end());
  for (int i = 0; i < L.size(); i++) {
    f[L[i]] = true;
  }

  vector<int> p;
  for (int i = 0; i < n; i++) {
    if (!f[i])
      p.push_back(i + 1);
  }

  cout << p.size() << endl;
  for (int i = 0; i < p.size(); i++) {
    cout << p[i] << endl;
  }
}