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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int INF = 1000000007;

int N, M, ans = 0;
int g[111][111];
vector<int> adj[111];
vector<int> homura;

void update() {
  if (homura.size() == 1)
    return;
  int res = 0;
  for (int i = 0; i < homura.size(); ++i) {
    int madoka = INF;
    for (int j = 0; j < homura.size(); ++j) {
      if (i == j)
        continue;
      madoka = min(madoka, g[homura[i]][homura[j]]);
    }
    res += madoka;
  }
  ans = max(ans, res);
}

inline bool check(int v) {
  for (int i = 0; i < homura.size(); ++i)
    if (!g[homura[i]][v])
      return 0;
  return 1;
}

void dfs(int u) {
  homura.push_back(u);
  update();
  int v;
  for (int i = 0; i < adj[u].size(); ++i) {
    v = adj[u][i];
    if (v < u)
      continue;
    if (check(v))
      dfs(v);
  }
  homura.pop_back();
}

int main() {
  int u, v, d;
  scanf("%d%d", &N, &M);
  while (M--) {
    scanf("%d%d%d", &u, &v, &d);
    g[u][v] = g[v][u] = d;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (u = 1; u <= N; ++u)
    dfs(u);
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1012345678;
#define pb push_back
#define sz(x) (int)x.size()
int rd() {
  int x;
  scanf("%d", &x);
  return x;
}
#define clr(x, a) memset(x, a, sizeof(x))
const int maxn = 110;
const int maxe = 100000;

int g[maxn][maxn], ans, n, m, maxx[maxn];
int r[maxn];
void clq(int maxr[], int rcnt, int b[], int bcnt) {
  int i, j, k;
  int maxt[maxn], tcnt = 0, v[maxn], vn = 0;

  if (bcnt == 0) {
    return;
  }

  for (i = 0; i < bcnt; i++) {
    vn = 0, tcnt = 0;
    int flag = 0;
    for (j = 0; j < rcnt; j++)
      if (g[r[j]][b[i]] == 0) {
        flag = 1;
        break;
      }
    if (!flag) {
      int all = 0;
      int tp = inf;
      int sum = 0;
      for (j = 0; j < rcnt; j++) {
        maxt[j] = maxr[j];
        if (g[r[j]][b[i]] != 0 && g[r[j]][b[i]] < maxr[j])
          maxt[j] = g[r[j]][b[i]];
        tp = min(tp, g[r[j]][b[i]]);
        sum += maxt[j];
      }
      r[rcnt] = b[i];
      maxt[rcnt] = tp;
      sum += tp;
      for (j = 0; j < bcnt; j++)
        if (i != j && g[b[i]][b[j]]) {
          all += maxx[b[j]];
          v[vn++] = b[j];
        }
      if (all + sum > ans) {
        ans = max(ans, sum);
        clq(maxt, rcnt + 1, v, vn);
      }
    }
  }
}

int main() {
  // freopen("in.txt","r",stdin);
  int i, j, k;
  while (scanf("%d%d", &n, &m) != EOF) {
    int x, y, z;

    for (i = 0; i < m; i++) {
      scanf("%d%d%d", &x, &y, &z);
      g[x][y] = g[y][x] = z;
      maxx[x] = max(maxx[x], z);
      maxx[y] = max(maxx[y], z);
    }

    int maxr[maxn], b[maxn];
    ans = 0;
    for (i = n; i >= 1; i--) {
      k = 0;
      int sum = maxx[i];
      for (j = i; j <= n; j++)
        if (i != j && g[i][j]) {
          b[k++] = j;
          sum += maxx[j];
        }
      r[0] = i;
      maxr[0] = inf;
      if (sum > ans)
        clq(maxr, 1, b, k);
    }
    if (n == 1) {
      printf("0\\n");
    } else {
      printf("%d\\n", ans);
    }
  }
}