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

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

struct E {
  int cost, to;
  E(int _to, int _cost) {
    cost = _cost;
    to = _to;
  }
};

const int MAX = 100;
vector<E> G[MAX];
int G2[MAX][MAX];
set<set<int>> S;
int res;
int N, M;

void func(set<int> &st) {
  vector<int> vec(st.begin(), st.end());
  if (S.count(st))
    return;
  S.insert(st);
  int sum = 0;
  for (int i = 0; i < vec.size(); i++) {
    int cost = 1e8;
    for (int j = 0; j < vec.size(); j++) {
      if (i == j)
        continue;
      cost = min(cost, G2[vec[i]][vec[j]]);
    }
    if (cost == 1e8)
      cost = 0;
    sum += cost;
  }
  res = max(res, sum);
  for (int i = 0; i < vec.size(); i++) {
    for (int j = 0; j < G[vec[i]].size(); j++) {
      int to = G[vec[i]][j].to;
      if (st.count(to))
        continue;
      bool flag = true;
      for (int k = 0; k < vec.size(); k++) {
        if (G2[to][vec[k]] == 0) {
          flag = false;
          break;
        }
      }
      if (flag) {
        set<int> n_st = st;
        n_st.insert(to);
        func(n_st);
      }
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    G[i].clear();
    for (int j = 0; j < N; j++) {
      G2[i][j] = 0;
    }
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(E(b, c));
    G[b].push_back(E(a, c));
    G2[a][b] = G2[b][a] = c;
  }

  res = 0;
  for (int i = 0; i < N; i++) {
    set<int> st;
    st.insert(i);
    func(st);
  }
  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

struct E {
  int cost, to;
  E(int _to, int _cost) {
    cost = _cost;
    to = _to;
  }
};

const int MAX = 100;
vector<E> G[MAX];
int G2[MAX][MAX];
int res;
int N, M;

void func(set<int> &st) {
  vector<int> vec(st.begin(), st.end());
  int sum = 0;
  for (int i = 0; i < vec.size(); i++) {
    int cost = 1e8;
    for (int j = 0; j < vec.size(); j++) {
      if (i == j)
        continue;
      cost = min(cost, G2[vec[i]][vec[j]]);
    }
    if (cost == 1e8)
      cost = 0;
    sum += cost;
  }
  res = max(res, sum);
  /*
  for(int i = 0 ; i < vec.size() ; i++){
    cout << vec[i] << ' ';
  }
  cout << endl;
  cout << "res = " << res << " sum = " << sum << endl;
  */

  for (int i = 0; i < vec.size(); i++) {
    for (int j = 0; j < G[vec[i]].size(); j++) {
      int to = G[vec[i]][j].to;
      if (st.count(to))
        continue;
      bool flag = true;
      for (int k = 0; k < vec.size(); k++) {
        if (G2[to][vec[k]] == 0) {
          flag = false;
          break;
        }
      }
      if (flag) {
        set<int> n_st = st;
        n_st.insert(to);
        func(n_st);
      }
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    G[i].clear();
    for (int j = 0; j < N; j++) {
      G2[i][j] = 0;
    }
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(E(b, c));
    G[b].push_back(E(a, c));
    G2[a][b] = G2[b][a] = c;
  }

  res = 0;
  for (int i = 0; i < N; i++) {
    set<int> st;
    st.insert(i);
    func(st);
  }
  cout << res << endl;

  return 0;
}