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

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
#define N 101
#define INF 1e9
typedef pair<int, int> P;
vector<P> G[N];
int n, g[N][N];

vector<int> U, V;
int can[N], ans;
vector<bool> used(N);
set<vector<bool>> S;

int calc() {

  int res = 0;
  for (int i = 0; i < V.size(); i++) {
    int mn = INF;
    for (int j = 0; j < V.size(); j++)
      if (i != j)
        mn = min(mn, g[V[i]][V[j]]);
    res += mn;
  }
  if (V.size() == 1)
    return 0;
  return res;
}

void solve(int idx) {
  if (idx == U.size()) {
    ans = max(ans, calc());
    return;
  }
  solve(idx + 1);

  V.push_back(U[idx]);
  solve(idx + 1);
  V.pop_back();
}

void dfs(int pos) {
  int flg = 1;
  U.push_back(pos), used[pos] = 1;
  if (S.count(used)) {
    U.pop_back(), used[pos] = 0;
    return;
  }
  S.insert(used);

  for (int i = 0; i < G[pos].size(); i++)
    can[G[pos][i].f]++;

  for (int i = 0; i < G[pos].size(); i++) {
    int nx = G[pos][i].f;
    if (used[nx] || can[nx] != U.size())
      continue;
    flg = 0;
    dfs(nx);
  }
  if (flg)
    solve(0);
  U.pop_back(), used[pos] = 0;
  for (int i = 0; i < G[pos].size(); i++)
    can[G[pos][i].f]--;
}

int main() {
  int m;
  cin >> n >> m;
  for (int i = 0, a, b, c; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(P(b, c));
    G[b].push_back(P(a, c));
    g[a][b] = g[b][a] = c;
  }

  for (int i = 0; i < n; i++)
    dfs(i);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
#define N 101
#define INF 1e9
typedef pair<int, int> P;
vector<P> G[N];
int n, er[N];

vector<int> U;
int can[N], ans;
vector<bool> used(N, 0);
set<vector<bool>> S;

int calc() {
  int res = 0;
  for (int i = 0; i < n; i++) {
    int mn = INF;
    if (!er[i] && used[i])
      for (int j = 0; j < G[i].size(); j++)
        if (!er[G[i][j].f] && used[G[i][j].f])
          mn = min(mn, G[i][j].s);
    res += (mn != INF) * mn;
  }
  return res;
}

int solve() {
  //  for(int i=0;i<U.size();i++)cout<<U[i]<<" ";cout<<endl;
  memset(er, 0, sizeof(er));
  int r = calc();
  for (int i = 0; i < n; i++) {
    int idx = 0, sum = 0;
    for (int j = 0, res; j < n; j++) {
      if (er[j])
        continue;
      er[j] = 1, res = calc(), er[j] = 0;
      if (sum < res)
        sum = res, idx = j;
    }
    er[idx] = 1;
    r = max(r, sum);
  }
  return r;
}

void dfs(int pos) {

  int flg = 1;
  U.push_back(pos), used[pos] = 1;
  if (S.count(used)) {
    U.pop_back(), used[pos] = 0;
    return;
  }
  //  S.insert(used);

  for (int i = 0; i < G[pos].size(); i++)
    can[G[pos][i].f]++;

  for (int i = 0; i < G[pos].size(); i++) {
    int nx = G[pos][i].f;
    if (used[nx] || can[nx] != U.size())
      continue;
    flg = 0;
    dfs(nx);
  }
  ans = max(ans, calc());
  U.pop_back(), used[pos] = 0;
  for (int i = 0; i < G[pos].size(); i++)
    can[G[pos][i].f]--;
}

int main() {
  int m;
  cin >> n >> m;
  for (int i = 0, a, b, c; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(P(b, c));
    G[b].push_back(P(a, c));
  }

  for (int i = 0; i < n; i++)
    dfs(i);
  cout << ans << endl;
  return 0;
}