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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16

ll n, m;
ll f[111][111];
vector<ll> crt;

ll calc_score(vector<ll> vs) {
  if (vs.size() < 2)
    return 0;
  ll res = 0;
  rep(i, vs.size()) {
    ll mins = INF;
    rep(j, vs.size()) {
      if (i == j)
        continue;
      minch(mins, f[vs[i]][vs[j]]);
    }
    res += mins;
  }
  return res;
}

ll dfs(ll v) {
  // dbg(v);
  ll res = calc_score(crt);
  repl(i, v + 1, n) {
    bool ok = true;
    for (ll u : crt) {
      if (i != u && f[i][u] == 0) {
        ok = false;
      }
    }
    if (ok) {
      crt.push_back(i);
      maxch(res, dfs(i));
      crt.pop_back();
    }
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  memset(f, 0, sizeof(f));
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    f[a][b] = f[b][a] = c;
  }
  ll res = 0;
  rep(i, n) {
    crt.push_back(i);
    maxch(res, dfs(i));
    crt.pop_back();
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16

ll n, m;
ll f[111][111];

ll calc_score(vector<ll> vs) {
  if (vs.size() < 2)
    return 0;
  ll res = 0;
  rep(i, vs.size()) {
    ll mins = INF;
    rep(j, vs.size()) {
      if (i == j)
        continue;
      minch(mins, f[vs[i]][vs[j]]);
    }
    res += mins;
  }
  return res;
}

ll dfs(ll v, vector<ll> crt) {
  if (v == n)
    return calc_score(crt);
  ll res = calc_score(crt);
  bool ok = true;
  for (ll u : crt) {
    if (f[v][u] == 0) {
      ok = false;
    }
  }
  maxch(res, dfs(v + 1, crt));
  if (ok) {
    crt.push_back(v);
    maxch(res, dfs(v + 1, crt));
    crt.pop_back();
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  memset(f, 0, sizeof(f));
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    f[a][b] = f[b][a] = c;
  }
  cout << dfs(0, vector<ll>()) << endl;
  return 0;
}