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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define SZ(a) (sizeof(a))
#define FILL(a, v) fill((int *)a, (int *)(a + (SZ(a) / SZ(*(a)))), v)
#define SORT(c) sort((c).begin(), (c).end())

bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 2147483647;

string A[5010];
bool d[5010][5010];
vector<P> xa;
int s[1010], t[1010];

int main() {
  int n, m;
  cin >> n;
  REP(i, n) {
    int x;
    cin >> A[i] >> x;
    xa.push_back(P(x, i));
  }
  cin >> m;
  REP(i, m) {
    string S, T;
    cin >> S >> T;
    REP(j, n) {
      if (A[j] == S)
        s[i] = j;
      if (A[j] == T)
        t[i] = j;
    }
  }

  REP(i, m) {
    d[s[i]][t[i]] = true;
    d[t[i]][s[i]] = true;
  }

  sort(xa.begin(), xa.end());

  bool f = true;
  while (f) {
    f = false;
    REP(i, n) {
      REP(j, n) {
        if (d[xa[i].second][xa[j].second]) {
          if (xa[i].first != xa[j].first)
            f = true;
          int Min = min(xa[i].first, xa[j].first);
          xa[i].first = Min;
          xa[j].first = Min;
        }
      }
    }
    sort(xa.begin(), xa.end());
  }

  int ans = 0;
  REP(i, n)
  ans += xa[i].first;

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define SZ(a) (sizeof(a))
#define FILL(a, v) fill((int *)a, (int *)(a + (SZ(a) / SZ(*(a)))), v)
#define SORT(c) sort((c).begin(), (c).end())

bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 2147483647;

string A[5010];
int x[5010];
int d[5010][5010];
string S[1010], T[1010];
int a[5010];
int s[1010], t[1010];

int main() {
  int n, m;
  cin >> n;
  REP(i, n)
  cin >> A[i] >> x[i];

  cin >> m;
  REP(i, m)
  cin >> S[i] >> T[i];

  REP(i, n) a[i] = i;

  REP(i, n) {
    REP(j, m) {
      if (A[i] == S[j])
        s[j] = i;
      if (A[i] == T[j])
        t[j] = i;
    }
  }

  REP(i, m) {
    d[s[i]][t[i]] = 1;
    d[t[i]][s[i]] = 1;
  }

  bool f = true;
  while (f) {
    f = false;
    REP(i, n - 1) {
      if (x[i] > x[i + 1]) {
        swap(x[i], x[i + 1]);
        swap(a[i], a[i + 1]);
        f = true;
      }
    }
  }

  REP(i, n) {
    REP(j, n) {
      if (d[i][j]) {
        int Min = min(x[i], x[j]);
        x[i] = Min;
        x[j] = Min;
      }
    }
  }

  int ans = 0;
  REP(i, n)
  ans += x[i];

  cout << ans << endl;

  return 0;
}