Problem p00817 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
#define INF 100000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, p1, p2;
vector<P> e[600];
vector<int> out;
vector<int> par;
bool saw[600];
bool see[600];
int dp[601][301];
P pre[1001];

P dfs(int v, bool ac) {
  saw[v] = true;
  P ret = P(1, 0);
  if (ac)
    ret.second++;
  rep(i, e[v].size()) {
    int to = e[v][i].first, id = e[v][i].second;
    if (saw[to])
      continue;
    if (id == 1) {
      P q = dfs(to, ac);
      ret.first += q.first;
      ret.second += q.second;
    } else {
      P q = dfs(to, ac ^ 1);
      ret.first += q.first;
      ret.second += q.second;
    }
  }
  return ret;
}

void dfs2(int v, bool ac) {
  if (ac)
    out.push_back(v);
  see[v] = true;
  rep(i, e[v].size()) {
    int to = e[v][i].first, id = e[v][i].second;
    if (see[to])
      continue;
    if (id == 1) {
      dfs2(to, ac);
    } else {
      dfs2(to, ac ^ 1);
    }
  }
}

void solve() {
  rep(i, 600) e[i].clear();
  memset(saw, 0, sizeof(saw));
  memset(see, 0, sizeof(see));
  memset(dp, 0, sizeof(dp));
  out.clear();
  par.clear();
  rep(i, n) {
    int f, t;
    string judge;
    cin >> f >> t >> judge;
    f--;
    t--;
    if (f == t)
      continue;
    if (judge == "yes") {
      e[f].push_back(P(t, 1));
      e[t].push_back(P(f, 1));
    } else {
      e[f].push_back(P(t, 2));
      e[t].push_back(P(f, 2));
    }
  }
  int cnt = 0;
  dp[0][0] = 1;
  rep(i, p1 + p2) {
    if (saw[i])
      continue;
    par.push_back(i);
    pre[cnt] = dfs(i, true);
    P p = pre[cnt];
    cnt++;
    for (int j = 0; j <= p1 - p.second; j++) {
      dp[cnt][j + p.second] += dp[cnt - 1][j];
      if (dp[cnt][j + p.second] > 2)
        dp[cnt][j + p.second] = 2;
    }
    for (int j = 0; j <= p1 - (p.first - p.second); j++) {
      dp[cnt][j + p.first - p.second] += dp[cnt - 1][j];
      if (dp[cnt][j + p.first - p.second] > 2) {
        dp[cnt][j + p.first - p.second] = 2;
      }
    }
  }
  if (dp[cnt][p1] != 1) {
    cout << "no" << endl;
    return;
  }
  int now = p1;
  for (int i = cnt - 1; i >= 0; i--) {
    if (now >= pre[i].second && dp[i][now - pre[i].second] > 0) {
      now -= pre[i].second;
      dfs2(par[i], true);
    } else {
      now = now - (pre[i].first - pre[i].second);
      dfs2(par[i], false);
    }
  }
  sort(out.begin(), out.end());
  rep(i, out.size()) cout << out[i] + 1 << endl;
  cout << "end" << endl;
}

int main() {
  while (cin >> n >> p1 >> p2) {
    if (n == 0 && p1 == 0 && p2 == 0)
      break;
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
#define INF 100000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, p1, p2;
vector<P> e[300];
vector<int> out;
vector<int> par;
bool saw[300];
bool see[300];
int dp[601][301];
P pre[1001];

P dfs(int v, bool ac) {
  saw[v] = true;
  P ret = P(1, 0);
  if (ac)
    ret.second++;
  rep(i, e[v].size()) {
    int to = e[v][i].first, id = e[v][i].second;
    if (saw[to])
      continue;
    if (id == 1) {
      P q = dfs(to, ac);
      ret.first += q.first;
      ret.second += q.second;
    } else {
      P q = dfs(to, ac ^ 1);
      ret.first += q.first;
      ret.second += q.second;
    }
  }
  return ret;
}

void dfs2(int v, bool ac) {
  if (ac)
    out.push_back(v);
  see[v] = true;
  rep(i, e[v].size()) {
    int to = e[v][i].first, id = e[v][i].second;
    if (see[to])
      continue;
    if (id == 1) {
      dfs2(to, ac);
    } else {
      dfs2(to, ac ^ 1);
    }
  }
}

void solve() {
  rep(i, 300) e[i].clear();
  memset(saw, 0, sizeof(saw));
  memset(see, 0, sizeof(see));
  memset(dp, 0, sizeof(dp));
  out.clear();
  par.clear();
  rep(i, n) {
    int f, t;
    string judge;
    cin >> f >> t >> judge;
    f--;
    t--;
    if (f == t)
      continue;
    if (judge == "yes") {
      e[f].push_back(P(t, 1));
      e[t].push_back(P(f, 1));
    } else {
      e[f].push_back(P(t, 2));
      e[t].push_back(P(f, 2));
    }
  }
  int cnt = 0;
  dp[0][0] = 1;
  rep(i, p1 + p2) {
    if (saw[i])
      continue;
    par.push_back(i);
    pre[cnt] = dfs(i, true);
    P p = pre[cnt];
    cnt++;
    for (int j = 0; j <= p1 - p.second; j++) {
      dp[cnt][j + p.second] += dp[cnt - 1][j];
      if (dp[cnt][j + p.second] > 2)
        dp[cnt][j + p.second] = 2;
    }
    for (int j = 0; j <= p1 - (p.first - p.second); j++) {
      dp[cnt][j + p.first - p.second] += dp[cnt - 1][j];
      if (dp[cnt][j + p.first - p.second] > 2) {
        dp[cnt][j + p.first - p.second] = 2;
      }
    }
  }
  if (dp[cnt][p1] != 1) {
    cout << "no" << endl;
    return;
  }
  int now = p1;
  for (int i = cnt - 1; i >= 0; i--) {
    if (now >= pre[i].second && dp[i][now - pre[i].second] > 0) {
      now -= pre[i].second;
      dfs2(par[i], true);
    } else {
      now = now - (pre[i].first - pre[i].second);
      dfs2(par[i], false);
    }
  }
  sort(out.begin(), out.end());
  rep(i, out.size()) cout << out[i] + 1 << endl;
  cout << "end" << endl;
}

int main() {
  while (cin >> n >> p1 >> p2) {
    if (n == 0 && p1 == 0 && p2 == 0)
      break;
    solve();
  }
}