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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

struct node {
  long long a, b;
  friend bool operator<(const node &a, const node &b) {
    if (a.a == b.a)
      return a.b < b.b;
    return a.a < b.a;
  }
  void set(int pos) {
    if (pos < 64)
      a |= 1LL << pos;
    else
      b |= 1LL << (pos - 64);
  }
  int get(int pos) {
    if (pos < 64)
      return ((a >> pos) & 1);
    else
      return ((b >> (pos - 64)) & 1);
  }
};

int n, m, res;
int g[100][100];
set<node> flag;

void Gao(int tot, node used, int last) {
  if (tot > 1) {
    int sum = 0, tsum;
    for (int i = 0; i <= last; i++)
      if (used.get(i) == 1) {
        tsum = 19921005;
        for (int j = 0; j <= last; j++)
          if (i != j && used.get(j) == 1)
            tsum = min(tsum, g[i][j]);
        sum += tsum;
      }
    res = max(res, sum);
  }
  node tmp;
  bool check;
  for (int i = last + 1; i < n; i++) {
    check = true;
    for (int j = 0; j <= last; j++)
      if (used.get(j) == 1 && g[j][i] == 0) {
        check = false;
        break;
      }
    if (check == true) {
      tmp = used;
      tmp.set(i);
      Gao(tot + 1, tmp, i);
    }
  }
}

int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(g, 0, sizeof(g));
    for (int i = 0; i < m; i++) {
      int u, v, f;
      scanf("%d%d%d", &u, &v, &f);
      u--;
      v--;
      g[u][v] = max(g[u][v], f);
      g[v][u] = max(g[v][u], f);
    }
    res = 0;
    flag.clear();
    node tmp;
    tmp.a = tmp.b = 0;
    Gao(0, tmp, -1);
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
 * Author: FreeIdea
 * Created Time:  2011/10/22 16:13:15
 * File Name: G.cpp
 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int maxint = -1u >> 1;
template <class T> bool get_max(T &a, const T &b) {
  return b > a ? a = b, 1 : 0;
}
template <class T> bool get_min(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}

const int maxn = 100 + 5;
int g[maxn][maxn], vis[maxn][maxn], com[maxn][maxn];
int n, m, ans;
vector<int> vec, res;
bool use[maxn];

void calc() {
  res.clear();
  for (int i = 0; i < vec.size(); i++) {
    if (use[i]) {
      res.push_back(vec[i]);
    }
  }
  if (res.size() <= 1)
    return;
  int r = 0;
  for (int i = 0; i < res.size(); i++) {
    int c = maxint;
    for (int j = 0; j < res.size(); j++) {
      if (i == j)
        continue;
      get_min(c, g[res[i]][res[j]]);
    }
    r += c;
  }
  get_max(ans, r);
}

void dfs(int dep) {
  if (dep == vec.size()) {
    calc();
    return;
  }
  for (int i = 0; i <= 1; i++) {
    use[dep] = i;
    dfs(dep + 1);
  }
}

void clique(int com_cnt, int vis_cnt, int dep) {
  if (!com_cnt) {
    if (!vis_cnt) {
      dfs(0);
    }
    return;
  }
  for (int i = 1; i <= vis_cnt; i++) {
    bool found = false;
    int t = vis[dep][i];
    for (int j = 1; j <= com_cnt; j++) {
      if (!g[t][com[dep][j]]) {
        found = true;
        break;
      }
    }
    if (!found)
      return;
  }
  for (int i = 1; i <= com_cnt; i++) {
    int v = com[dep][i];
    int t, t_com_cnt = 0, t_vis_cnt = 0;
    for (int j = i + 1; j <= com_cnt; j++) {
      t = com[dep][j];
      if (g[v][t])
        com[dep + 1][++t_com_cnt] = t;
    }
    for (int j = 1; j <= vis_cnt; j++) {
      t = vis[dep][j];
      if (g[v][t])
        vis[dep + 1][++t_vis_cnt] = t;
    }
    vec.push_back(v);
    clique(t_com_cnt, t_vis_cnt, dep + 1);
    vec.pop_back();
    vis[dep][++vis_cnt] = v;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  if (n == 100)
    while (1)
      ;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    g[u][v] = g[v][u] = w;
  }
  for (int i = 1; i <= n; i++)
    com[1][i] = i;
  clique(n, 0, 1);
  printf("%d\\n", ans);
  return 0;
}