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

#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int maxn = 501;
struct Edge {
  int u, v, f, next;
} edge[maxn];
int head[maxn], ednum;
void add(int u, int v, int f) {
  edge[ednum].u = u;
  edge[ednum].v = v;
  edge[ednum].f = f;
  edge[ednum].next = head[u];
  head[u] = ednum++;
}
int n, m, mp[maxn][maxn];
struct Bitset {
  bitset<100> state;
  bool operator==(const Bitset &p) const {
    for (int i = 99; i >= 0; --i)
      if (state[i] != p.state[i])
        return false;
    return true;
  }
  bool operator<(const Bitset &p) const {
    for (int i = 99; i >= 0; --i)
      if (state[i] != p.state[i])
        return state[i] < p.state[i];
  }
} tmp;
set<Bitset> vis;
queue<Bitset> q;
bool check(int u, Bitset a) {
  for (int j = 0; j < 99; ++j)
    if (a.state[j] && !mp[u][j + 1])
      return false;
  return true;
}
int ans;
int match(Bitset a) {
  int j, k, res = 0, tp;
  for (j = 0; j < 100; ++j) {
    if (!a.state[j])
      continue;
    tp = 1e9 + 100;
    for (k = 0; k < 100; ++k) {
      if (j == k)
        continue;
      if (!a.state[k])
        continue;
      tp = min(tp, mp[j + 1][k + 1]);
    }
    if (tp != 1e9 + 100)
      res += tp;
  }
  return res;
}
void bfs(int u) {
  int i;
  Bitset now, tmp;
  now.state.reset();
  q.push(now);
  vis.insert(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    for (i = 1; i <= 100; ++i) {
      if (now.state[i - 1])
        continue;
      if (!check(i, now))
        continue;
      tmp = now;
      tmp.state[i - 1] = 1;
      if (vis.find(tmp) == vis.end()) {
        vis.insert(tmp);
        ans = max(ans, match(tmp));
        q.push(tmp);
      }
    }
  }
}
int main() {
  int i, j, u, v, f;
  scanf("%d%d", &n, &m);
  // n=100,m=0;
  memset(head, -1, sizeof(head));
  ednum = 0;
  for (i = 0; i < m; ++i) {
    scanf("%d%d%d", &u, &v, &f);
    add(u, v, f);
    add(v, u, f);
    mp[u][v] = mp[v][u] = f;
  }
  ans = 0;
  bfs(0);
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000007
#define MP make_pair
#define FI first
#define SE second
#define PB push_back
#define VI vector<int>
const double EPS = 1e-7, PI = acos(-1.0);
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
template <class T> inline T IN(T &num) {
  num = 0;
  char c = getchar(), f = 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = 1, c = getchar();
  while ('0' <= c && c <= '9')
    num = num * 10 + c - '0', c = getchar();
  if (f)
    num = -num;
  return num;
}
//------------------------   code   ---------------------------------//
const int NUM = 100010;
const int MAXV = 100 + 10, MAXE = 100 * 2 + 10;
int cost[MAXV][MAXV];
struct node {
  LL a, b;
  node() { a = b = 0; }
  void set(int x) {
    if (x < 64)
      a |= (1LL << x);
    else
      b |= (1LL << (x - 64));
  }
  void reset(int x) {
    if (x < 64)
      a &= ~(1LL << x);
    else
      b &= ~(1LL << (x - 64));
  }
  bool get(int x) {
    if (x < 64)
      return (a & (1LL << x));
    else
      return (b & (1LL << (x - 64)));
  }
  bool operator<(const node &x) const {
    if (a == x.a)
      return b < x.b;
    return a < x.a;
  }
};

int n, m;
set<node> st;
int ans = 0;
void solve(node vis) {
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis.get(i))
      continue;
    int mn = INF;
    for (int j = 1; j <= n; ++j) {
      if (!vis.get(j) || i == j)
        continue;
      mn = min(mn, cost[i][j]);
    }
    if (mn != INF)
      res += mn;
  }
  ans = max(ans, res);
}
void dfs(node vis) {
  for (int i = 1; i <= n; ++i) {
    if (vis.get(i))
      continue;
    vis.set(i);
    node tmp = vis;
    for (int j = 1; j <= n; ++j) {
      if (vis.get(j) && cost[i][j] == 0)
        vis.reset(i);
    }
    if (st.find(tmp) == st.end()) {
      solve(tmp);
      st.insert(tmp);
      dfs(tmp);
    }
  }
}
int main() {
#ifdef ACM_TEST
  freopen("in.txt", "r", stdin);
//	freopen("in.txt", "w", stdout);
#endif
  int u, v, w;
  IN(n), IN(m);
  for (int i = 0; i < m; ++i) {
    IN(u), IN(v), IN(w);
    cost[u][v] = w;
    cost[v][u] = w;
  }
  node vis;
  dfs(vis);
  printf("%d\\n", ans);
  return 0;
}