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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

VVI G(101);
int is[101][101];

int dfs(int i, int num, VI &xs) {
  if (SZ(xs) == num) {
    int res = 0;
    for (int j = 0; j < num; ++j) {
      int tmp = 1e9;
      for (int k = 0; k < num; ++k) {
        if (j == k)
          continue;
        if (!is[xs[j]][xs[k]])
          return 0;
        tmp = min(is[xs[j]][xs[k]], tmp);
      }
      res += tmp;
    }
    return res;
  }

  int res = 0;
  for (int to : G[i]) {
    if (find(ALL(xs), to) != xs.end() || to <= i)
      continue;
    xs.PB(to);
    res = max(res, dfs(to, num, xs));
    xs.pop_back();
  }

  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    --u, --v;
    G[u].PB(v);
    G[v].PB(u);
    is[u][v] = is[v][u] = c;
  }

  int ans = 0;
  for (int i = 2; i <= min(N, 15); ++i) {
    VI xs;
    REP(u, N) {
      xs.PB(u);
      ans = max(ans, dfs(u, i, xs));
      xs.pop_back();
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

VVI G(101);
int is[101][101];

int dfs(int i, int num, VI &xs) {
  if (SZ(xs) == num) {
    int res = 0;
    for (int j = 0; j < num; ++j) {
      int tmp = 1e9;
      for (int k = 0; k < num; ++k) {
        if (j == k)
          continue;
        if (!is[xs[j]][xs[k]])
          return 0;
        tmp = min(is[xs[j]][xs[k]], tmp);
      }
      res += tmp;
    }
    return res;
  }

  int res = 0;
  for (int to : G[i]) {
    if (find(ALL(xs), to) != xs.end())
      continue;
    xs.PB(to);
    res = max(res, dfs(to, num, xs));
    xs.pop_back();
  }

  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    --u, --v;
    G[u].PB(v);
    G[v].PB(u);
    is[u][v] = is[v][u] = c;
  }

  int ans = 0;
  for (int i = 2; i <= min(N, 15); ++i) {
    VI xs;
    REP(u, N) {
      xs.PB(u);
      ans = max(ans, dfs(u, i, xs));
      xs.pop_back();
    }
  }
  cout << ans << endl;

  return 0;
}