Problem p00817 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 305
#define MAX_E 1005

struct edge {
  int to, cost;
};
vector<edge> G[MAX_V];
int V, E, A, B;
vector<vector<int>> t, u;

bool visited[MAX_V];
vector<int> a, b;

void dfs(int pos, bool flg) {
  if (visited[pos])
    return;
  if (flg)
    a.push_back(pos);
  else
    b.push_back(pos);
  visited[pos] = true;
  for (int i = 0; i < (int)G[pos].size(); i++) {
    bool f = G[pos][i].cost;
    dfs(G[pos][i].to, f ^ (!flg));
  }
}

void make_data() {
  for (int i = 0; i < MAX_V; i++)
    visited[i] = false;
  for (int i = 0; i < V; i++) {
    if (visited[i])
      continue;
    a.clear();
    b.clear();
    dfs(i, true);
    t.push_back(a);
    u.push_back(b);
    /*
    for(int j=0;j<(int)a.size();j++)
      cout<<a[j]+1<<",";
    cout<<"=";

    for(int j=0;j<(int)b.size();j++)
      cout<<b[j]+1<<",";
    cout<<endl;
    */
  }
  // cout<<"---"<<endl;
}

void solve() {
  make_data();
  int prev[MAX_V][MAX_V];
  int dp[MAX_V][MAX_V], N = t.size();
  for (int i = 0; i < MAX_V; i++)
    for (int j = 0; j < MAX_V; j++)
      dp[i][j] = 0;
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    int a = t[i].size(), b = u[i].size();
    for (int j = 0; j < MAX_V; j++) {
      if (dp[i][j] == 0)
        continue;
      if (j + a < MAX_V) {
        dp[i + 1][j + a] += dp[i][j];
        prev[i + 1][j + a] = j;
      }
      if (j + b < MAX_V) {
        dp[i + 1][j + b] += dp[i][j];
        prev[i + 1][j + b] = j;
      }
    }
  }

  if (dp[N][A] != 1) {
    cout << "no" << endl;
    return;
  }

  vector<int> ans;
  int j = A;
  for (int i = N; i > 0; i--) {
    int nj = prev[i][j];
    if (j - nj == 0)
      continue;
    if (j - nj == (int)t[i - 1].size()) {
      for (int k = 0; k < j - nj; k++)
        ans.push_back(t[i - 1][k]);
    } else {
      for (int k = 0; k < j - nj; k++)
        ans.push_back(u[i - 1][k]);
    }
    j = nj;
  }

  sort(ans.begin(), ans.end());
  for (int i = 0; i < (int)ans.size(); i++)
    cout << ans[i] + 1 << endl;
  cout << "end" << endl;
}

void init() {
  for (int i = 0; i < MAX_V; i++) {
    t.clear();
    u.clear();
    G[i].clear();
  }
  t.clear();
}

int main() {
  while (cin >> E >> A >> B) {
    if (E == 0 && A == 0 && B == 0)
      break;
    init();
    V = A + B;
    for (int i = 0; i < E; i++) {
      int a, b;
      string str;
      cin >> a >> b >> str;
      a--, b--;
      G[a].push_back((edge){b, (str == "yes")});
      G[b].push_back((edge){a, (str == "yes")});
    }
    // make_data();
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 605
#define MAX_E 1005

struct edge {
  int to, cost;
};
vector<edge> G[MAX_V];
int V, E, A, B;
vector<vector<int>> t, u;

bool visited[MAX_V];
vector<int> a, b;

void dfs(int pos, bool flg) {
  if (visited[pos])
    return;
  if (flg)
    a.push_back(pos);
  else
    b.push_back(pos);
  visited[pos] = true;
  for (int i = 0; i < (int)G[pos].size(); i++) {
    bool f = G[pos][i].cost;
    dfs(G[pos][i].to, f ^ (!flg));
  }
}

void make_data() {
  for (int i = 0; i < MAX_V; i++)
    visited[i] = false;
  for (int i = 0; i < V; i++) {
    if (visited[i])
      continue;
    a.clear();
    b.clear();
    dfs(i, true);
    t.push_back(a);
    u.push_back(b);
    /*
    for(int j=0;j<(int)a.size();j++)
      cout<<a[j]+1<<",";
    cout<<"=";

    for(int j=0;j<(int)b.size();j++)
      cout<<b[j]+1<<",";
    cout<<endl;
    */
  }
  // cout<<"---"<<endl;
}

void solve() {
  make_data();
  int prev[MAX_V][MAX_V];
  int dp[MAX_V][MAX_V], N = t.size();
  for (int i = 0; i < MAX_V; i++)
    for (int j = 0; j < MAX_V; j++)
      dp[i][j] = 0;
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    int a = t[i].size(), b = u[i].size();
    for (int j = 0; j < MAX_V; j++) {
      if (dp[i][j] == 0)
        continue;
      if (j + a < MAX_V) {
        dp[i + 1][j + a] += dp[i][j];
        prev[i + 1][j + a] = j;
      }
      if (j + b < MAX_V) {
        dp[i + 1][j + b] += dp[i][j];
        prev[i + 1][j + b] = j;
      }
    }
  }

  if (dp[N][A] != 1) {
    cout << "no" << endl;
    return;
  }

  vector<int> ans;
  int j = A;
  for (int i = N; i > 0; i--) {
    int nj = prev[i][j];
    if (j - nj == 0)
      continue;
    if (j - nj == (int)t[i - 1].size()) {
      for (int k = 0; k < j - nj; k++)
        ans.push_back(t[i - 1][k]);
    } else {
      for (int k = 0; k < j - nj; k++)
        ans.push_back(u[i - 1][k]);
    }
    j = nj;
  }

  sort(ans.begin(), ans.end());
  for (int i = 0; i < (int)ans.size(); i++)
    cout << ans[i] + 1 << endl;
  cout << "end" << endl;
}

void init() {
  for (int i = 0; i < MAX_V; i++) {
    t.clear();
    u.clear();
    G[i].clear();
  }
  t.clear();
}

int main() {
  while (cin >> E >> A >> B) {
    if (E == 0 && A == 0 && B == 0)
      break;
    init();
    V = A + B;
    for (int i = 0; i < E; i++) {
      int a, b;
      string str;
      cin >> a >> b >> str;
      a--, b--;
      G[a].push_back((edge){b, (str == "yes")});
      G[b].push_back((edge){a, (str == "yes")});
    }
    // make_data();
    solve();
  }
  return 0;
}