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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define PB push_back
#define FT first
#define SD second
#define MP make_pair
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int N = 105, MOD = 7 + 1e9;
int a[20], G[N][N];
int n, m, ans, cnt;
int solve() {
  if (cnt < 2)
    return 0;
  int res = 0;
  for (int i = 0; i < cnt; i++) {
    int _min = INF;
    for (int j = 0; j < cnt; j++) {
      if (i == j)
        continue;
      _min = min(_min, G[a[i]][a[j]]);
    }
    res += _min;
  }
  return res;
}
void dfs(int u) {
  ans = max(ans, solve());
  for (int i = u; i <= n; i++) {
    bool fg = 1;
    for (int j = 0; j < cnt; j++) {
      int v = a[j];
      if (G[i][v] == 0) {
        fg = 0;
        break;
      }
    }
    if (fg) {
      a[cnt++] = i;
      dfs(i + 1);
      cnt--;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  ans = 0;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    G[u][v] = G[v][u] = w;
    ans = max(ans, w * 2);
  }
  dfs(1);
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define LL long long

//?自己加油O(∩_∩)O~

const long long mod = 1000000007; //?个必?是?数
int q[111][111];
bitset<102> w[111];
bitset<102> duilie[4200000];
int haha[4200000];
int n, m;
int ans = 0;
map<string, bool> vis;
void BFS() {
  for (int j = 0; j < n; j++)
    duilie[j][j] = 1;
  int fron = 0, rear = n;
  while (fron < rear) {
    ans = max(ans, haha[fron]);
    vector<int> v;
    v.clear();
    for (int j = 0; j < n; j++)
      if (duilie[fron][j])
        v.push_back(j);
    for (int j = 0; j < n; j++) {
      if ((w[j] & duilie[fron]) == duilie[fron]) {
        duilie[rear] = duilie[fron];
        string z = "";
        for (int k = 0; k < n; k++) {
          char hh;
          if (duilie[fron][k] || k == j)
            hh = '1';
          else
            hh = '0';
          z += hh;
        }
        if (vis[z])
          continue;
        vis[z] = 1;
        haha[rear] = 0;
        v.push_back(j);
        for (int k = 0; k < v.size(); k++) {
          int g = 2100000000;
          for (int l = 0; l < v.size(); l++) {
            if (k == l)
              continue;
            g = min(g, q[v[k]][v[l]]);
          }
          haha[rear] += g;
        }
        duilie[rear++][j] = 1;
      }
    }
    fron++;
  }
}
int main() {

  scanf("%d%d", &n, &m);
  for (int j = 0; j < m; j++) {
    int g, h, f;
    scanf("%d%d%d", &g, &h, &f);
    g--;
    h--;
    q[g][h] = f;
    q[h][g] = f;
  }
  for (int j = 0; j < n; j++) {
    for (int k = 0; k < n; k++) {
      if (q[j][k]) {
        w[j][k] = 1;
        w[k][j] = 1;
      }
    }
  }
  BFS();
  printf("%d\\n", ans);
  return 0;
}