Problem p02837 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define printvec(vec)                                                          \
  for (auto &&item : vec)                                                      \
    cout << item << " ";                                                       \
  cout << endl

using namespace std;
using i64 = long long;

// 仮定が矛盾しないか判定
bool ishonest(int person, vector<int> const &honest_list,
              vector<map<int, int>> const &evidence) {
  if (honest_list[person] == 0)
    return true;
  bool judge = true;
  for (auto &&mp : evidence[person]) {
    if (honest_list[mp.first] != mp.second)
      judge = false;
  }
  return judge;
}

int recurse(int person, vector<int> &honest_list,
            vector<map<int, int>> const &evidence) {
  if (person == evidence.size() - 1) {
    bool judge = true;
    int counter = 0;
    for (int i = 1; i <= evidence.size(); i++) {
      judge &= ishonest(i, honest_list, evidence);
      if (!judge)
        return 0;
      if (honest_list[i] == 1)
        counter++;
    }
    return counter;
  }

  honest_list[person + 1] = 1;
  int honest = recurse(person + 1, honest_list, evidence);
  honest_list[person + 1] = 0;
  int unkind = recurse(person + 1, honest_list, evidence);
  return max(honest, unkind);
}

int main() {
  int n;
  cin >> n;

  vector<map<int, int>> evidence(n + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    for (int j = 1; j <= a; j++) // a-times
    {
      int x, y;
      cin >> x >> y;
      evidence[i][x] = y;
    }
  }

  int ans;

  // 仮定
  vector<int> honest_list(n + 1, -1);

  honest_list[1] = 1;
  int honest = recurse(1, honest_list, evidence);
  honest_list[1] = 0;
  int unkind = recurse(1, honest_list, evidence);
  ans = max(honest, unkind);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define printvec(vec)                                                          \
  for (auto &&item : vec)                                                      \
    cout << item << " ";                                                       \
  cout << endl

using namespace std;
using i64 = long long;

// 仮定が矛盾しないか判定
bool ishonest(int person, vector<int> const &honest_list,
              vector<map<int, int>> const &evidence) {
  if (honest_list[person] == 0)
    return true;
  bool judge = true;
  for (auto &&mp : evidence[person]) {
    if (honest_list[mp.first] != mp.second)
      judge = false;
  }
  return judge;
}

int recurse(int person, vector<int> &honest_list,
            vector<map<int, int>> const &evidence) {
  if (person == evidence.size() - 1) {
    bool judge = true;
    int counter = 0;
    for (int i = 1; i <= evidence.size() - 1; i++) {
      judge &= ishonest(i, honest_list, evidence);
      if (!judge)
        return 0;
      if (honest_list[i] == 1)
        counter++;
    }
    return counter;
  }

  honest_list[person + 1] = 1;
  int honest = recurse(person + 1, honest_list, evidence);
  honest_list[person + 1] = 0;
  int unkind = recurse(person + 1, honest_list, evidence);
  return max(honest, unkind);
}

int main() {
  int n;
  cin >> n;

  vector<map<int, int>> evidence(n + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    for (int j = 1; j <= a; j++) // a-times
    {
      int x, y;
      cin >> x >> y;
      evidence[i][x] = y;
    }
  }

  int ans;

  // 仮定
  vector<int> honest_list(n + 1, -1);

  honest_list[1] = 1;
  int honest = recurse(1, honest_list, evidence);
  honest_list[1] = 0;
  int unkind = recurse(1, honest_list, evidence);
  ans = max(honest, unkind);

  cout << ans << endl;

  return 0;
}