Problem p01423 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a, 0, sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5 + 10;

int n, m, mp[105][105], vis[105], ans;
vector<int> Q;

void dfs(int u) {
  int tmp = 0;
  for (int i = 0; i < (int)Q.size(); i++) {
    int minn = INF;
    for (int j = 0; j < (int)Q.size(); j++) {
      if (i != j)
        minn = min(minn, mp[Q[i]][Q[j]]);
    }
    if (minn == INF)
      minn = 0;
    tmp += minn;
  }
  ans = max(ans, tmp);

  for (int v = u + 1; v <= n; v++) {
    if (vis[v])
      continue;
    int flag = 1;
    for (
        int j = 0; j < (int)Q.size();
        j++) { //?????????v???????????????????????????????????????v?°±???????????¨??????????????????
      if (mp[v][Q[j]] == 0) {
        flag = 0;
        break;
      }
    }

    if (flag) {
      vis[v] = 1; // ???v
      Q.push_back(v);
      dfs(v);
      vis[v] = 0; // ??????v  ?????? ???v??????????????????????°?
      Q.pop_back();
    }
  }
}

int main() {
  n = read(), m = read();
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    mp[u][v] = mp[v][u] = w;
  }

  for (int i = 1; i <= n; i++) {
    vis[i] = 1;
    Q.push_back(i); // ?????????????????????????????????
    dfs(0);
    Q.pop_back();
    vis[i] = 0;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a, 0, sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5 + 10;

int n, m, mp[20][20], vis[20], ans;
vector<int> Q;

void dfs(int u) {
  int tmp = 0;
  for (int i = 0; i < (int)Q.size(); i++) {
    int minn = INF;
    for (int j = 0; j < (int)Q.size(); j++) {
      if (i != j)
        minn = min(minn, mp[Q[i]][Q[j]]);
    }
    if (minn == INF)
      minn = 0;
    tmp += minn;
  }
  ans = max(ans, tmp);

  for (int v = u + 1; v <= n; v++) {
    if (vis[v])
      continue;
    int flag = 1;
    for (
        int j = 0; j < (int)Q.size();
        j++) { //?????????v???????????????????????????????????????v?°±???????????¨??????????????????
      if (mp[v][Q[j]] == 0) {
        flag = 0;
        break;
      }
    }

    if (flag) {
      vis[v] = 1; // ???v
      Q.push_back(v);
      dfs(v);
      vis[v] = 0; // ??????v  ?????? ???v??????????????????????°?
      Q.pop_back();
    }
  }
}

int main() {
  n = read(), m = read();
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    mp[u][v] = mp[v][u] = w;
  }

  for (int i = 1; i <= n; i++) {
    vis[i] = 1;
    Q.push_back(i); // ?????????????????????????????????
    dfs(0);
    Q.pop_back();
    vis[i] = 0;
  }

  cout << ans << endl;

  return 0;
}