Problem:
Problem G:
True Liars
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fortunate to remember a legend of the foggy island, which he had heard from patriarchs in his childhood. This must be the island in the legend.
In the legend, two tribes have inhabited the island, one is divine and the other is devilish; once members of the divine tribe bless you, your future is bright and promising, and your soul will eventually go to Heaven; in contrast, once members of the devilish tribe curse you, your future is bleak and hopeless, and your soul will eventually fall down to Hell.
In order to prevent the worst-case scenario, Akira should distinguish the devilish from the divine. But how? They looked exactly alike and he could not distinguish one from the other solely by their appearances. He still had his last hope, however. The members of the divine tribe are truth-tellers, that is, they always tell the truth and those of the devilish tribe are liars, that is, they always tell a lie.
He asked some of the whether or not some are divine. They knew one another very much and always responded to him "faithfully" according to their individual natures (i.e., they always tell the truth or always a lie). He did not dare to ask any other forms of questions, since the legend says that a devilish member would curse a person forever when he did not like the question. He had another piece of useful information: the legend tells the populations of both tribes. These numbers in the legend are trustworthy since everyone living on this island is immortal and none have ever been born at least these millennia.
You are a good computer programmer and so requested to help Akira by writing a program that classifies the inhabitants according to their answers to his inquiries.
Input
The input consists of multiple data sets, each in the following format:
n
p
1
p
2
x
1
y
1
a
1
x
2
y
2
a
2
...
x
i
y
i
a
i
...
x
n
y
n
a
n
The first line has three non-negative integers
n
,
p
1
, and
p
2
.
n
is the number of questions Akira asked.
p
1
and
p
2
are the populations of the divine and devilish tribes, respectively, in the legend. Each of the following
n
lines has two integers
x
i
,
y
i
and one word
a
i
.
x
i
and
y
i
are the identification numbers of inhabitants, each of which is between 1 and
p
1
+
p
2
, inclusive.
a
i
is either "yes", if the inhabitant
x
i
said that the inhabitant
y
i
was a member of the divine tribe, or "no", otherwise. Note that
x
i
and
y
i
can be the same number since "are you a member of the divine tribe?" is a valid question. Note also that two lines may have the same
x
's and
y
's since Akira was very upset and might have asked the same question to the same one more than once.
You may assume that
n
is less than 1000 and that
p
1
and
p
2
are less than 300. A line with three zeros, i.e., "0 0 0", represents the end of the input. You can assume
...(truncated)

Submitted solutions:

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int N = 2010;
int fa[N], siz[N], a[N], f[N][N], pre[N][N], ans[N], b[N], c[N]; 
bool vis[N], in[N];
int findfa(int x) {
	return fa[x] == x ? x : fa[x] = findfa(fa[x]);
}

void merge(int x, int y) {
	x = findfa(x); y = findfa(y);
	if (x == y) return ;
	fa[x] = y; siz[y] += siz[x];
}

int main() {
	int n, p1, p2, m;
	while (~scanf("%d%d%d", &n, &p1, &p2) && (n | p1 | p2)) {
		memset(f, 0, sizeof(f));
		memset(vis, 0, sizeof(vis));
		memset(in, 0, sizeof(in));
		memset(pre, 0, sizeof(pre));
		m = p1 + p2;
		for (int i = 1; i <= m * 2; i++) fa[i] = i;
		for (int i = 1; i <= m; i++) siz[i] = 1, siz[i + m] = 0;
		for (int i = 1, x, y; i <= n; i++) {
			char s[10];
			scanf("%d%d%s", &x, &y, s);
			if (s[0] == 'y') merge(x, y), merge(x + m, y + m);
			else merge(x + m, y), merge(x, y + m);
		}
		int cnt = 0;
		for (int i = 1; i <= m; i++) {
			int x = findfa(i), y = findfa(i + m);
			if (vis[x] || vis[y]) continue ;
			a[++cnt] = x; b[cnt] = y;
			vis[x] = vis[y] = 1;
		}
//		for (int i = 1; i <= cnt; i++)
//			printf("%d%c", a[i], " \n"[i == cnt]);
//		for (int i = 1; i <= cnt; i++)
//			printf("%d%c", b[i], " \n"[i == cnt]);
		f[0][0] = 1;
		for (int i = 1; i <= cnt; i++)
			for (int j = 0; j <= m; j++) 
				if (f[i - 1][j]) {
					f[i][j + siz[a[i]]] += f[i - 1][j], pre[i][j + siz[a[i]]] = j;
					f[i][j + siz[b[i]]] += f[i - 1][j], pre[i][j + siz[b[i]]] = j;
				}
		if (p1 == p2) puts("no");
		else {
			if (f[cnt][p1] == 1) {
				int k = 0, s = p1;
				for (int i = cnt; i >= 1; i--)
					if (s - siz[a[i]] == pre[i][s]) in[a[i]] = 1, s -= siz[a[i]];
					else in[b[i]] = 1, s -= siz[b[i]];
//				for (int i = 1; i <= m; i++)
//					printf("%d%c", findfa(i), " \n"[i == m]);
//				for (int i = 1; i <= m * 2; i++)
//					printf("%d%c", in[i], " \n"[i == m * 2]);
				for (int i = 1; i <= m; i++)
					if (in[findfa(i)]) ans[++k] = i;
				for (int i = 1; i <= k; i++)
					printf("%d\n", ans[i]);
				puts("end");
			}
			else puts("no");
		}
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 34724
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG

vector<int> P;                // parent
vector<int> divine;           // 0:not_checked, 1:true, -1:lie
map<int, vector<int>> groups; // <根、葉>
int m;

// 未完成

void init() {
  groups.clear();
  P = vector<int>(m + 1);
  divine = vector<int>(m + 1);
  for (int i = 1; i <= m; i++) {
    P[i] = i;
    groups[i].push_back(i);
  }
}
int root(int c) {
  if (P[c] == c)
    return c;
  return (P[c] = root(P[c]));
}
bool is_same_set(int a, int b) { return root(a) == root(b); }
// 子のgroupに入っている要素を親のgroupに移し替える。
void unite(int c, int p) {
  groups[root(p)].insert(groups[root(p)].end(), groups[root(c)].begin(),
                         groups[root(c)].end());
  groups.erase(root(c));
  P[root(c)] = root(p);
}
// 結合して子になるグループのdivineをすべて裏返してから結合
void r_unite(int c, int p) {
  for (int i : groups[root(c)]) {
    divine[i] *= -1;
  }
  unite(c, p);
}

int main() {
  int n, t, l;
  while (cin >> n >> t >> l) {
    if (!n && !t && !l)
      break;

    m = t + l;
    init();
    /* cout << m << endl; */
    // グループ分け
    for (int i = 0; i < n; i++) {
      int x, y;
      string a;
      cin >> x >> y >> a;
      int honest = (a == "yes") ? 1 : -1;
      if (x == y || is_same_set(x, y))
        continue;
      int reverse;
      if (divine[x]) {
        reverse = divine[x] * honest;
      } else {
        reverse = honest;
        divine[x] = 1;
      }
      assert(reverse != 0);
      if (divine[y] && reverse != divine[y])
        r_unite(y, x);
      else {
        unite(y, x);
        divine[y] = reverse;
      }
      assert(divine[y] != 0);
    }
    // test------------
    /* for (int i : groups[1]) { */
    /*   cout << i << ' ' << divine[i] << endl; */
    /* } */
    //----------------
    map<int, pair<int, int>> pair_t_l; //<root of group, <truth_teller, liar>>
    for (auto group = groups.begin(); group != groups.end(); ++group) {
      int t_teller = 0, liar = 0;
      for (int i : group->second) {
        if (divine[i] == 1)
          t_teller++;
        // 言及されていない人は嘘つきとみなす。
        else { // bug:{}をつけないと行を加えたときにつけ忘れる。
          divine[i] = -1;
          liar++;
        }
      }
      /* cout << t_teller << ' ' << liar << endl; */
      pair_t_l[group->first] = make_pair(t_teller, liar);
    }

    int num_g = groups.size();
    vector<vector<int>> dp(
        num_g + 1,
        vector<int>(
            t + 1, 0)); // dp[i][j]:i個のグループでj人の正直者を何通りつくれるか
    vector<vector<set<int>>> p_rev(
        num_g + 1, vector<set<int>>(t + 1)); //裏返るグループを保存する配列
    int gc = 0;
    // 動的計画法
    for (auto P = pair_t_l.begin(); P != pair_t_l.end(); ++P) {
      gc++;
      pair<int, int> p = P->second; //<truth_teller, liar>
      /* // もし正直者と嘘つきの数が同じなら答えはnoなので抜ける */
      /* if (p.first == p.second) { */
      /*   dp[num_g][t] = 10; */
      /*   break; */
      /* } */
      if (gc == 1) {
        dp[gc][p.first]++;
        dp[gc][p.second]++;
        /* cout << p.second << endl; */
        if (p.second <= t)
          p_rev.at(gc).at(p.second).insert(P->first);
      }
      for (int j = 0; j <= t; j++) { // bug:jを1から始めるとt=0のとき間違う
        /* cout << gc << ' ' << j << ' ' << p.first << ' ' << p.second << ' ' */
        /*      << dp[gc - 1][j] << endl; */
        if (p.first + j <= t) {
          dp[gc][p.first + j] += dp[gc - 1][j];
          if (dp[gc - 1][j]) {
            for (auto group : p_rev[gc - 1][j]) {
              p_rev[gc][p.first + j].insert(group);
            }
          }
        }
        if (p.second + j <= t) {
          dp[gc][p.second + j] += dp[gc - 1][j];
          /* assert(!(gc==2&&p.second+j==4)); */
          if (dp[gc - 1][j]) {
            for (auto group : p_rev[gc - 1][j]) {
              p_rev[gc][p.second + j].insert(group);
            }
            p_rev[gc][p.second + j].insert(P->first);
          }
          /* cout<<gc<<' '<<p.second+j<<' '<<dp[gc][p.second + j]<<endl; */
        }
      }
    }

    /* for (int i : divine) */
    /*   cout << i << endl; */
    vector<int> ans;
    for (int group : p_rev[num_g][t]) {
      /* cout<<1<<endl; */
      /* assert(groups.count(1)==0); */
      for (int person : groups[group]) {
        divine[person] *= -1;
      }
    }
    for (auto group = groups.begin(); group != groups.end(); ++group) {
      /* cout << group->first << ' '; */
      for (int person : group->second) {
        /* cout << person << ' '; */
        if (divine[person] == 1)
          ans.push_back(person);
      }
      /* cout << endl; */
    }
    sort(ans.begin(), ans.end());
    /* cout << groups.size() << endl; */
    /* cout << root(24) << endl; */

    if (dp[num_g][t] != 1) {
      cout << "no" << endl;
    } else {
      for (int i : ans)
        cout << i << endl;
      cout << "end" << endl;
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 14724
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int short

using vint = vector<int>;

struct edge {
	int to, kind;
	edge(){}
	edge(int to, int kind):to(to), kind(kind){}
};

using Graph = vector<vector<edge> >;

struct UF {
	vint data;
	UF(){}
	void init(int n) {
		data.clear();
		data.resize(n, -1);
	}
	int find(int x) {
		return data[x] < 0 ? x : data[x] = find(data[x]);	 
	}
	int size(int x) {
		return -data[find(x)];
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if(x == y) return;
		if(data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
	}
};

int Q, A, B, N;
Graph graph;
Graph rgraph;
UF uf;

vint col[2];
vint divines[2];

void fillcolor(int s, int c) {
	queue<int> que;
	que.push(s);
	col[c][s] = c;
	if(!c) ++divines[c][s];
	while(!que.empty()) {
		int u = que.front(); que.pop();
		for(edge e : graph[u]) {
			if(col[c][e.to] == -1) {
				que.push(e.to);
				col[c][e.to] = col[c][u]^e.kind;
				if(!col[c][e.to]) ++divines[c][s];
			} else if(col[c][e.to] != (col[c][u]^e.kind)) {
				divines[c][s] = -1;				
				return;
			}
		}
		for(edge e : rgraph[u]) {
			if(col[c][e.to] == -1) {
				que.push(e.to);
				col[c][e.to] = col[c][u]^e.kind;
				if(!col[c][e.to]) ++divines[c][s];				
			} else if(col[c][e.to] != (col[c][u]^e.kind)) {
				divines[c][s] = -1;
				return;
			}
		}
	}
	//cout << s << " " << c << " " << divines[c][s] << endl;
}

void init() {
	graph.clear();
	graph.resize(N);
	rgraph.clear();
	rgraph.resize(N);
	col[0].clear();
	col[0].resize(N, -1);
	col[1].clear();
	col[1].resize(N, -1);
	divines[0].clear();
	divines[0].resize(N);
	divines[1].clear();
	divines[1].resize(N);
	uf.init(N);
}

using Pi = pair<int, int>;

//int dp[303][303][303];
Pi rev[303][303][303];

signed main() {
	while(cin >> Q >> A >> B, Q || A || B) {
		N = A + B;		
		init();
		for(int i = 0; i < Q; ++i) {
			int a, b;
			string s;
			cin >> a >> b >> s;
			--a, --b;
			graph[a].emplace_back(b, s == "no");
			rgraph[b].emplace_back(a, s == "no");
			uf.unite(a, b);
		}
		//map<int, int> mp;
		vint mp(N);
		int sz = 0;
		for(int i = 0; i < N; ++i) {
			if(uf.find(i) != i) continue;
			mp[sz++] = i;
			for(int j = 0; j < 2; ++j) {
				fillcolor(i, j);
			}
		}
		for(int i = 0; i < 303; ++i) {
			for(int j = 0; j < 303; ++j) {
				for(int k = 0; k < 303; ++k) {
					//dp[i][j][k] = 0;
					rev[i][j][k] = Pi(-2, -2);
				}
			}
		}
		//dp[0][0][0] = 1;
		rev[0][0][0] = Pi(-1, -1);
		for(int i = 0; i < sz; ++i) {
			for(int j = 0; j <= A; ++j) {
				for(int k = 0; k <= B; ++k) {
					//if(!dp[i][j][k]) continue;
					if(rev[i][j][k].first == -2) continue;					
					for(int l = 0; l < 2; ++l) {
						if(divines[l][mp[i]] == -1) continue;
						int x = divines[l][mp[i]];
						int y = uf.size(mp[i])-x;
						if(j+x > A || k+y > B) continue;
						//if(!dp[i+1][j+x][k+y]) {
						if(rev[i+1][j+x][k+y].first == -2) {
							//dp[i+1][j+x][k+y] = 1;
							rev[i+1][j+x][k+y] = Pi(mp[i], l);
						} else {
							//dp[i+1][j+x][k+y] = 2;
							rev[i+1][j+x][k+y] = Pi(-1, -1);
						}
					}
				}
			}
		}
		//if(dp[sz][A][B] != 1) {
		if(rev[sz][A][B].first < 0) {		
			cout << "no" << endl;
			continue;
		} else {
			vint ans;
			int a = A, b = B;
			for(int i = sz; i > 0; --i) {
				Pi p = rev[i][a][b];
				for(int j = 0; j < N; ++j) {
					if(uf.find(j) != p.first) continue;
					if(!col[p.second][j]) ans.push_back(j);
				}
				a -= divines[p.second][p.first];
				b -= (uf.size(p.first)-divines[p.second][p.first]);
			}
			sort(ans.begin(), ans.end());
			for(int id : ans) cout << id+1 << endl;
			cout << "end" << endl;
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 111736
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int short

using vint = vector<int>;

struct edge {
	int to, kind;
	edge(){}
	edge(int to, int kind):to(to), kind(kind){}
};

using Graph = vector<vector<edge> >;

struct UF {
	vint data;
	UF(){}
	void init(int n) {
		data.clear();
		data.resize(n, -1);
	}
	int find(int x) {
		return data[x] < 0 ? x : data[x] = find(data[x]);	 
	}
	int size(int x) {
		return -data[find(x)];
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if(x == y) return;
		if(data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
	}
};

int Q, A, B, N;
Graph graph;
Graph rgraph;
UF uf;

vint col[2];
vint divines[2];

void fillcolor(int s, int c) {
	queue<int> que;
	que.push(s);
	col[c][s] = c;
	if(!c) ++divines[c][s];
	while(!que.empty()) {
		int u = que.front(); que.pop();
		for(edge e : graph[u]) {
			if(col[c][e.to] == -1) {
				que.push(e.to);
				col[c][e.to] = col[c][u]^e.kind;
				if(!col[c][e.to]) ++divines[c][s];
			} else if(col[c][e.to] != (col[c][u]^e.kind)) {
				divines[c][s] = -1;				
				return;
			}
		}
		for(edge e : rgraph[u]) {
			if(col[c][e.to] == -1) {
				que.push(e.to);
				col[c][e.to] = col[c][u]^e.kind;
				if(!col[c][e.to]) ++divines[c][s];				
			} else if(col[c][e.to] != (col[c][u]^e.kind)) {
				divines[c][s] = -1;
				return;
			}
		}
	}
	//cout << s << " " << c << " " << divines[c][s] << endl;
}

void init() {
	graph.clear();
	graph.resize(N);
	rgraph.clear();
	rgraph.resize(N);
	col[0].clear();
	col[0].resize(N, -1);
	col[1].clear();
	col[1].resize(N, -1);
	divines[0].clear();
	divines[0].resize(N);
	divines[1].clear();
	divines[1].resize(N);
	uf.init(N);
}

using Pi = pair<int, int>;

//int dp[303][303][303];
Pi rev[303][303][303];

signed main() {
	while(cin >> Q >> A >> B, Q || A || B) {
		N = A + B;		
		init();
		for(int i = 0; i < Q; ++i) {
			int a, b;
			string s;
			cin >> a >> b >> s;
			--a, --b;
			graph[a].emplace_back(b, s == "no");
			rgraph[b].emplace_back(a, s == "no");
			uf.unite(a, b);
		}
		//map<int, int> mp;
		vint mp(N);
		int sz = 0;
		for(int i = 0; i < N; ++i) {
			if(uf.find(i) != i) continue;
			mp[sz++] = i;
			for(int j = 0; j < 2; ++j) {
				fillcolor(i, j);
			}
		}
		for(int i = 0; i < 303; ++i) {
			for(int j = 0; j < 303; ++j) {
				for(int k = 0; k < 303; ++k) {
					//dp[i][j][k] = 0;
					rev[i][j][k] = Pi(-2, -2);
				}
			}
		}
		//dp[0][0][0] = 1;
		rev[0][0][0] = Pi(-1, -1);
		for(int i = 0; i < sz; ++i) {
			for(int j = 0; j <= A; ++j) {
				for(int k = 0; k <= B; ++k) {
					//if(!dp[i][j][k]) continue;
					if(rev[i][j][k].first == -2) continue;					
					for(int l = 0; l < 2; ++l) {
						if(divines[l][mp[i]] == -1) continue;
						int x = divines[l][mp[i]];
						int y = uf.size(mp[i])-x;
						if(j+x > A || k+y > B) continue;
						//if(!dp[i+1][j+x][k+y]) {
						if(rev[i+1][j+x][k+y].first == -2) {
							//dp[i+1][j+x][k+y] = 1;
							rev[i+1][j+x][k+y] = Pi(mp[i], l);
						} else {
							//dp[i+1][j+x][k+y] = 2;
							rev[i+1][j+x][k+y] = Pi(-1, -1);
						}
					}
				}
			}
		}
		//if(dp[sz][A][B] != 1) {
		if(rev[sz][A][B].first < 0) {		
			cout << "no" << endl;
			continue;
		} else {
			vint ans;
			int a = A, b = B;
			for(int i = sz; i > 0; --i) {
				Pi p = rev[i][a][b];
				for(int j = 0; j < N; ++j) {
					if(uf.find(j) != p.first) continue;
					if(!col[p.second][j]) ans.push_back(j);
				}
				a -= divines[p.second][p.first];
				b -= (uf.size(p.first)-divines[p.second][p.first]);
			}
			sort(ans.begin(), ans.end());
			for(int id : ans) cout << id+1 << endl;
			cout << "end" << endl;
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 111768
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
//??°????????¨
#define MAX_N 800
typedef pair<int,int>P;
vector<P>G[MAX_N];

bool used[MAX_N];
vector<int> color[2][MAX_N+1];

void init(){
	REP(i,MAX_N){
		G[i].clear();
		used[i]=false;
		color[0][i].clear();
		color[1][i].clear();
	}
}

void biparate(int v,int num,int c){
	if(used[v]==true)return;
	used[v]=true;
	color[c][num].push_back(v);
	REP(i,G[v].size()){
		biparate(G[v][i].first,num,(c+G[v][i].second)%2);
	}
}

#define NONE 0
#define UNI 1
#define MUL 2

int main(){
	int n,p1,p2;
	while(cin>>n>>p1>>p2,n+p1+p2){
		init();
		REP(i,n){
			int x,y;
			string s;
			cin>>x>>y>>s;
			x--;y--;
			int z=0;
			if(s=="no")z=1;//no????????¢
			G[x].push_back(P(y,z));
			G[y].push_back(P(x,z));
		}
		//?????¨??°??????
		{
			int num=0;
			REP(i,p1+p2){
				if(used[i]==false){
					biparate(i,num,0);
					num++;
				}
			}
		}
		//??????????????????
		vector<int>np[MAX_N+2];
		//vector<vector<int>>back(MAX_N);
		REP(i,MAX_N+1){
			np[0].push_back(NONE);
		}
		np[0][0]=UNI;
		REP(i,MAX_N){
			if((color[0][i].size()==0)&&(color[1][i].size()==0)){
				np[i+1]=np[i];
				continue;
			}
			REP(j,MAX_N+1){
				np[i+1].push_back(NONE);
			}
			REP(j,MAX_N){
				//0?????\??????
				if(np[i][j]==UNI){
					if(np[i+1][j+color[0][i].size()]==NONE){
						np[i+1][j+color[0][i].size()]=UNI;
					}else{
						//if(color[0][i].size()!=0)
						np[i+1][j+color[0][i].size()]=MUL;
					}
				}
				if(np[i][j]==MUL){
					np[i+1][j+color[0][i].size()]=MUL;
				}
				//1?????\??????
				if(np[i][j]==UNI){
					if(np[i+1][j+color[1][i].size()]==NONE){
						np[i+1][j+color[1][i].size()]=UNI;
					}else{
						//if(color[1][i].size()!=0)
						np[i+1][j+color[1][i].size()]=MUL;
					}
					
				}
				if(np[i][j]==MUL){
					np[i+1][j+color[1][i].size()]=MUL;
				}
			}
		}
		
		
		if(np[MAX_N][p1]==UNI){
			vector<int>ans;
			int now=p1;
			for(int i=MAX_N;i>=0;i--){
				if((color[0][i].size()==0)&&(color[1][i].size()==0))continue;
				//cout<<now<<endl;
				if(np[i][now-color[0][i].size()]==1){
					REP(j,color[0][i].size()){
						ans.push_back(color[0][i][j]);
					}
					now-=color[0][i].size();
				}else{
					REP(j,color[1][i].size()){
						ans.push_back(color[1][i][j]);
					}
					now-=color[1][i].size();
				}
				
			}
			sort(ans.begin(),ans.end());
			REP(i,ans.size()){
				cout<<ans[i]+1<<endl;
			}
			cout<<"end"<<endl;
		}else if(np[MAX_N][p2]==UNI){
		    vector<int>ans;
			int now=p2;
			for(int i=MAX_N;i>=0;i--){
				if((color[0][i].size()==0)&&(color[1][i].size()==0))continue;
				//cout<<now<<endl;
				if(np[i][now-color[0][i].size()]==1){
					REP(j,color[1][i].size()){
						ans.push_back(color[1][i][j]);
					}
					now-=color[0][i].size();
				}else{
					REP(j,color[0][i].size()){
						ans.push_back(color[0][i][j]);
					}
					now-=color[1][i].size();
				}
				
			}
			sort(ans.begin(),ans.end());
			REP(i,ans.size()){
				cout<<ans[i]+1<<endl;
			}
			
			cout<<"end"<<endl;
		}else{
			cout<<"no"<<endl;
		}
		
	}
}
accuracy: 1
time_ms: 10
memory_kb: 5676
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;

struct edge {
    int to;
    bool yes;
};

using edges = vector<edge>;
using graph = vector<edges>;

// (divine, devilish)
pii dfs(graph const& g, int v, bool divine, vector<bool>& checked) {
    auto res = make_pair((int)divine, (int)!divine);
    checked[v] = true;
    for(auto& e : g[v]) {
        if(checked[e.to]) {
            continue;
        }
        bool next_state = (divine && e.yes || !divine && !e.yes);
        auto t = dfs(g, e.to, next_state, checked);
        res.first += t.first;
        res.second += t.second;
    }
    return res;
}

void determine(graph const& g, int v, bool divine, vector<int>& res) {
    res[v] = divine;
    for(auto& e : g[v]) {
        if(res[e.to] != -1) {
            continue;
        }
        bool next_state = (divine && e.yes || !divine && !e.yes);
        determine(g, e.to, next_state, res);
    }
}

struct data {
    int person_id;
    bool divine;
    pii each_num; // (divine, devilish)
    data() : person_id(-1), divine(false), each_num(make_pair(0, 0))
    {}
};

int main() {
    int n, p1, p2;
    while(cin >> n >> p1 >> p2, n || p1 || p2) {
        graph g(p1 + p2);
        if(n == 0) {
            if(p2 == 0) {
                for(int i = 1; i <= p1 + p2; ++i) {
                    cout << i << endl;
                }
            } else if(p1 != 0) {
                cout << "no" << endl;
                continue;
            }
            cout << "end" << endl;
            continue;
        }
        for(int i = 0; i < n; ++i) {
            int x, y;
            string a;
            cin >> x >> y >> a;
            g[x - 1].push_back(edge{y - 1, a == "yes"});
            g[y - 1].push_back(edge{x - 1, a == "yes"});
        }
        vector<vector<vector<data>>> dp(1, vector<vector<data>>(p1 + 1, vector<data>(p2 + 1)));
        dp[0][0][0].person_id = 0;
        vector<bool> checked(p1 + p2);
        for(int i = 0; i < p1 + p2; ++i) {
            auto now = dp.back();
            vector<vector<data>> nxt(p1 + 1, vector<data>(p2 + 1));
            if(!checked[i]) {
                for(int b = 0; b <= 1; ++b) {
                    data d;
                    d.person_id = i;
                    d.divine = b;
                    auto checked2 = checked;
                    d.each_num = dfs(g, i, b, checked);
                    if(b == 0) {
                        checked = checked2; // restore
                    }
                    for(int j = 0; j <= p1; ++j) {
                        for(int k = 0; k <= p2; ++k) {
                            if(now[j][k].person_id == -1) {
                                continue;
                            }
                            int nj = j + d.each_num.first, nk = k + d.each_num.second;
                            if(nj > p1 || nk > p2 || nxt[nj][nk].person_id == -2) {
                                continue;
                            }
                            if(nxt[nj][nk].person_id >= 0 || now[j][k].person_id == -2) { // ambiguous
                                nxt[nj][nk].person_id = -2;
                                continue;
                            }
                            nxt[nj][nk] = d;
                        }
                    }
                }
                dp.push_back(move(nxt));
            }
        }
        auto now = dp.back()[p1][p2];
        if(now.person_id < 0) {
            cout << "no" << endl;
        } else {
            vector<int> res(p1 + p2, -1);
            int P1 = p1, P2 = p2;
            for(int i = dp.size() - 1; i > 0; --i) {
                auto d = dp[i][P1][P2];
                determine(g, d.person_id, d.divine, res);
                P1 -= d.each_num.first;
                P2 -= d.each_num.second;
            }
            for(int i = 0; i < p1 + p2; ++i) {
                if(res[i] == 1) {
                    cout << i + 1 << endl;
                }
            }
            cout << "end" << endl;
        }
    }
}
accuracy: 1
time_ms: 150
memory_kb: 22876
final_rank: 10

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"


struct edge {
  int from;
  int to;
  bool same;
  edge(int f, int t, bool s):from(f), to(t), same(s) {

  }
  /*bool operator ==(edge &e) {
    return from == e.from&&to == e.to&&same == e.same;
  }*/
};

vector<int>types;
vector<vector<edge>>edges;
pair<vector<int>, vector<int>>dfs(const int now,const bool div) {
  types[now] = div;
  pair<vector<int>, vector<int>>apair;
  if (div)apair.first.push_back(now);
  else apair.second.push_back(now);
  for (auto e : edges[now]) {
    if (types[e.to] == -1) {
      const bool tdiv = e.same ? div : !div;
      auto p(dfs(e.to, tdiv));
      apair.first.insert(apair.first.end(), p.first.begin(), p.first.end());
      apair.second.insert(apair.second.end(), p.second.begin(), p.second.end());
    }
  }
  return apair;
}
struct d{
  int num;
  vector<bool>his;
  d() :num(0), his() {

  }
};
int main() {
  while (1) {
    edges.clear();
    types.clear();
    int N, P, Q; cin >> N >> P >> Q;
    if (!N&&!P&&!Q)break;
    edges.resize(P+Q);
    types.resize(P + Q);
    fill(types.begin(), types.end(), -1);
    vector<vector<int>>gyou(P + Q, vector<int>(P + Q, -1));
    for (int i = 0; i < N; ++i) {
      int x, y; string a; cin >> x >> y >> a;
      x--; y--;
      if (x == y)continue;
      bool same = a == "yes";
      gyou[x][y] = same;
      gyou[y][x] = same;

    }
    for (int i = 0; i < P + Q; ++i) {
      for (int j = 0; j < P + Q; ++j) {
        if (gyou[i][j] != -1) {
          edges[i].push_back(edge(i, j, gyou[i][j]));
        }
      }
    }
    vector<pair<vector<int>, vector<int>>>ps;
    for (int i = 0; i < P + Q; ++i) {
      if (types[i] == -1) {
        auto p = dfs(i, true);
        ps.push_back(p);
      }
    }
    //dp[???][?\???°]
    vector<vector<d>>dp(ps.size() + 1, vector<d>(P + Q +1));
    dp[0][0].num = 1;
    for (int t = 0; t < ps.size(); ++t) {
      for (int dnum = 0; dnum <= P + Q; ++dnum) {
        if (dp[t][dnum].num) {
          for (int i = 0; i < 2; ++i) {
            const int nextdnum = (!i)?dnum + ps[t].first.size():dnum+ps[t].second.size();
            vector<bool>nexthis(dp[t][dnum].his);
            nexthis.push_back(!i);
            dp[t + 1][nextdnum].num+=dp[t][dnum].num;
            if (dp[t + 1][nextdnum].num == 1) {
              dp[t + 1][nextdnum].his = nexthis;
            }
          }
          
        }
      }
    }
    if (dp[ps.size()][P].num == 1) {
      set<int>divs;
      vector<bool>his(dp[ps.size()][P].his);
      for (int t = 0; t < ps.size(); ++t) {
        vector<int>v;
        if (his[t]) {
          v = ps[t].first;
        }
        else {
          v = ps[t].second;
        }
        for (auto n : v) {
          divs.emplace(n);
        }
      }
      for (auto d : divs) {
        cout << d + 1 << endl;
      }
      cout << "end" << endl;
    }
    else {
      cout << "no" << endl;
    }
    
  }
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6244
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
#include<bitset>

using namespace std;

vector<int> edges[623][2];
bool used[623];
unsigned valid[623][623];
bitset<623> dp[623][623];
int dpx;
int temp[623];

void rec(int v,int p,int d){
  if(temp[v]){
    if(temp[v]!=d)throw -1;
  }else{
    temp[v]=d;
    for(int i=0;i<2;i++){
      for(auto e:edges[v][i]){
	if(v==p)continue;
	rec(e,v,(i?1:-1)*d);
      }
    }
  }
}

void srec(int v,int p){
  if(!used[v]++){
    for(int i=0;i<2;i++){
      for(auto e:edges[v][i]){
	if(e==p)continue;
	srec(e,v);
      }
    }
  }
}

bitset<623> record;

void dodp(int v){
  for(int i=0;i<2;i++){
    fill(begin(temp),end(temp),0);
    try{
      rec(v,-1,i?1:-1);
      bitset<623> r;
      for(int i=0;i<623;i++){
	r[i]=temp[i]==1;
      }
      int ndiv=r.count();
      for(int j=600;j-ndiv>=0;j--){
	if(valid[dpx][j-ndiv]){
	  if(valid[dpx+1][j]==0&&valid[dpx][j-ndiv]==1){
	    valid[dpx+1][j]=1;
	    dp[dpx+1][j]=dp[dpx][j-ndiv]|r;
	  }else{
	    valid[dpx+1][j]=2;
	  }
	}
      }
    }catch(...){}
  }
  srec(v,-1);
  dpx++;
}

int main(){
  for(int n,p1,p2;cin>>n>>p1>>p2,n|p1|p2;){
    for(auto &e:edges){
      for(auto &f:e){
	f.clear();
      }
    }
    fill(begin(used),end(used),false);
    fill(valid[0],valid[623],0);
    while(n--){
      int x,y;
      char a[9];
      cin>>x>>y>>a;
      edges[x][a[0]=='y'].push_back(y);
      edges[y][a[0]=='y'].push_back(x);
    }
    valid[0][0]=1;
    dpx=0;
    for(int i=1;i<=p1+p2;i++){
      if(!used[i]){
	dodp(i);
      }
    }
    if(valid[dpx][p1]!=1){
      cout<<"no"<<endl;
    }else{
      for(int i=1;i<=p1+p2;i++){
	if(dp[dpx][p1][i]){
	  cout<<i<<endl;
	}
      }
      cout<<"end"<<endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 33124
final_rank: 8

code_snippet: #include <string> 
#include <algorithm> 
#include <cfloat> 
#include <climits> 
#include <cmath> 
#include <complex> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <functional> 
#include <iostream> 
#include <map> 
#include <memory> 
#include <queue> 
#include <set> 
#include <sstream> 
#include <stack> 
#include <utility> 
#include <vector> 

#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(),(x).end() 
typedef long long ll;
using namespace std; 
const double eps = 1e-10;

struct UnionFind{
    vector<int> parent, rank;
    UnionFind(int n){ parent = vector<int>(n, -1); rank = vector<int>(n, 0); }
    int find(int x){
        if(parent[x] == -1) return x;
        else return (parent[x] = find(parent[x]));
    }
    bool unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return false;
        if(rank[x] < rank[y])
            parent[x] = y;
        else
            parent[y] = x;
        if(rank[x] == rank[y])
            ++rank[x];
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int dp[1010][1010], lst[1010][1010];
int main(){
    for(;;){
        int n, p1, p2;
        cin >> n >> p1 >> p2;
        if(n == 0 && p1 == 0 && p2==0) return 0;
        int m = p1 + p2;
        UnionFind uf(2 * m);
        for(int i=0;i<n;++i){
            int x, y;
            string s;
            cin >> x >> y >> s;
            --x; --y;
            if(s == "yes"){
                uf.unite(x, y);
                uf.unite(x+m, y+m);
            }
            else{
                uf.unite(x, y+m);
                uf.unite(x+m, y);
            }
        }
        map<int, int> mp;
        vector<int> v, w, z;
        map<int, vector<int> > va, vb;
        for(int i=0;i<m;++i){
            int a = uf.find(i);
            int b = uf.find(i + m);
            if(mp.find(a) != mp.end()){
                int pp = mp[a];
                if(a == uf.find(pp))
                    va[pp].push_back(i);
                else
                    vb[pp].push_back(i);
            }
            else{
                mp[a] = mp[b] = i;
                va[i].push_back(i);
            }
        }
        EACH(i, va){
            v.push_back(i->second.size());
            w.push_back(vb[i->first].size());
            z.push_back(i->first);
        }
        memset(dp, 0, sizeof(dp));
        memset(lst, -1, sizeof(lst));
        dp[0][0] = 1;
        for(int i=0;i<v.size();++i){
            for(int j=0;j<=m;++j){
                if(j >= v[i]){
                    dp[i+1][j] += dp[i][j-v[i]];
                    if(dp[i][j-v[i]])lst[i+1][j] = j-v[i];
                }
                if(j >= w[i]){
                    dp[i+1][j] += dp[i][j-w[i]];
                    if(dp[i][j-w[i]])lst[i+1][j] = j-w[i];
                }
            }
        }
        /*
        cout << p1 << endl;
        for(int i=0;i<=3;++i){
            for(int j=0;j<=4;++j){cout.width(3); cout << lst[i][j];}
            cout << endl;
        }
        */
        if(dp[(int)v.size()][p1] != 1)
            cout << "no" << endl;
        else{
            vector<int> f(m);
            int num = p1;
            for(int i=v.size()-1;i>=0;--i){
                int bnum = lst[i+1][num];
                if(bnum+v[i] == num){
                    EACH(j, va[z[i]]) f[*j] = 1;
                }
                else{
                    EACH(j, vb[z[i]]) f[*j] = 1;
                }
                num = bnum;
            }
            for(int i=0;i<m;++i){
                if(f[i]) cout << i+1 << endl;
            }
            cout << "end" << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 9264
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

class UnionFind {
private:
  vector<int> par, rank;
public:
  UnionFind(int n) {
    par = rank = vector<int>(n);
    for(int i = 0; i < n; ++i) par[i] = i;
  }

  int find(int x) {
    return par[x] == x ? x : par[x] = find(par[x]);
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if(rank[x] == rank[y]) ++rank[x];
    }
  }
};

int main() {
  for(int n, p1, p2; cin >> n >> p1 >> p2 && (n|p1|p2); ) {
    int m = p1+p2;
    UnionFind uf(m*2);
    for(int i = 0; i < n; ++i) {
      int x, y;
      string a;
      cin >> x >> y >> a;
      --x; --y;
      if(a == "yes") {
        uf.unite(x, y);
        uf.unite(x+m, y+m);
      } else {
        uf.unite(x, y+m);
        uf.unite(x+m, y);
      }
    }

    bool no_flag = false;
    map<int, pair<int, int> > group;
    for(int i = 0; i < m; ++i) {
      if(uf.same(i, i+m)) {
        no_flag = true;
        break;
      }
      int root = uf.find(i);
      int add = 1;
      if(root >= m) {
        root -= m;
        add = 0;
      }
      group[root].first++;
      group[root].second += add;
    }
    if(no_flag) {
      cout << "no" << endl;
      continue;
    }

    vector<int> roots;
    vector<pair<int, int> > v;
    for(map<int, pair<int,int> >::iterator it = group.begin();
        it != group.end(); ++it) {
      roots.push_back(it->first);
      v.push_back(make_pair(it->second.second, 
                            it->second.first - it->second.second));
    }

    int size = v.size();
    vector<vector<int> > dp(size+1, vector<int>(p1+1, 0));
    vector<vector<int> > prev(size+1, vector<int>(p1+1, -1));
    dp[0][0] = 1;
    for(int i = 0; i < v.size(); ++i) {
      for(int j = 0; j <= p1; ++j) {
        if(!dp[i][j]) continue;
        try {
          int nj = j + v[i].first;
          if(nj > p1) throw 0;
          dp[i+1][nj] += 1;
          prev[i+1][nj] = 0;
        } catch(...) {}
        try {
          int nj = j + v[i].second;
          if(nj > p1) throw 0;
          dp[i+1][nj] += 1;
          prev[i+1][nj] = 1;
        } catch(...) {}
      }
    }

    if(dp[v.size()][p1] != 1) {
      cout << "no" << endl;
      continue;
    }

    vector<int> r(m*2);
    {
      int i = v.size();
      int j = p1;
      while(prev[i][j] != -1) {
        if(prev[i][j] == 0) {
          r[roots[i-1]] = 1;
          j -= v[i-1].first;
        } else {
          r[roots[i-1]+m] = 1;
          j -= v[i-1].second;
        }
        --i;
      }
    }
    for(int i = 0; i < m; ++i) {
      if(r[uf.find(i)]) {
        cout << i+1 << endl;
      }
    }
    cout << "end" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1372
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

const int MAX_V = 666;

int N, P1, P2, M;
int d[MAX_V][MAX_V];
bool used[MAX_V];
int id[MAX_V];
int val[MAX_V];

typedef pair<int, int> P;
P dfs(int u, int c, int k){
    if(used[u]) return P(0, 0);
    used[u] = true;
    id[u] = k;
    val[u] = c;
    P res(0, 0);
    if(c == 0) res.first ++;
    if(c == 1) res.second ++;
    REP(v, M) if(d[u][v] >= 0){
        P p = dfs(v, c ^ d[u][v], k);
        res = P(res.first + p.first, res.second + p.second);
    }
    return res;
}

void add(int& x, int y){
    x = min(x + y, 2);
}

int main(){
    int N, P1, P2;
    while(cin >> N >> P1 >> P2 && !(N == 0 && P1 == 0 && P2 == 0)){
        M = P1 + P2;
        memset(d, -1, sizeof(d));
        REP(i, N){
            int x, y;
            string s;
            cin >> x >> y >> s;
            d[x - 1][y - 1] = d[y - 1][x - 1] = (s[0] == 'y' ? 0 : 1);
        }
        memset(used, 0, sizeof(used));
        int K = 0;
        vector<P> vp;
        REP(i, M) if(!used[i]){
            P p = dfs(i, 0, K++);
            vp.push_back(p);
        }

        int dp[MAX_V][MAX_V] = {};
        int back[MAX_V][MAX_V] = {};
        dp[0][0] = 1;
        for(int i = 0; i < vp.size(); i++){
            for(int j = M; j >= 0; j--){
                if(dp[i][j] >= 1){
                    add(dp[i + 1][j + vp[i].first], dp[i][j]);
                    add(dp[i + 1][j + vp[i].second], dp[i][j]);
                    back[i + 1][j + vp[i].first] = 0;
                    back[i + 1][j + vp[i].second] = 1;
                }
            }
        }
        if(dp[vp.size()][P1] == 1){
            int curr = P1;
            for(int i = vp.size() - 1; i >= 0; i--){
                if(back[i + 1][curr] == 0){
                    curr -= vp[i].first;
                }else{
                    curr -= vp[i].second;

                    for(int u = 0; u < M; u++)
                        if(id[u] == i)
                            val[u] ^= 1;
                }
            }
            for(int i = 0; i < M; i++){
                if(val[i] == 0){
                    cout << i + 1 << endl;
                }
            }
            cout << "end" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6432
final_rank: 5

code_snippet: #include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int q, n, m, p[1200], prev[610][610], dp[610][610];
int root(int x){
	if(p[x] == x) return x;
	return p[x] = root(p[x]);
}
void merge(int a, int b){
	a = root(a); b = root(b);
	if(a != b) p[b] = a;
}

int main(){
	while(cin >> q >> n >> m, q + n + m){
		rep(i, 2 * (n + m)) p[i] = i;
		rep(i, q){
			string c;
			int a, b;
			cin >> a >> b >> c;
			a--; b--;
			if(c == "yes") rep(j, 2) merge(2 * a + j, 2 * b + j);
			else rep(j, 2) merge(2 * a + j, 2 * b + 1 - j);
		}
		map<int, vi> s;
		vector<vi> ys, ns;
		vector<pi> v;
		vi ans; int p;
		bool use[1200] = {};
		
		rep(i, 2 * (n + m)) s[root(i)].pb(i);
		rep(i, 2 * (n + m)){
			int t = 0, l = 0, r = root(i);
			if(use[r / 2]) continue;
			use[r / 2] = 1;
			vi a, b;
			each(j, s[r]){
				if(*j % 2) l++, b.pb(*j / 2);
				else t++, a.pb(*j / 2);
			}
			v.pb(mp(t, l));
			ys.pb(a); ns.pb(b);
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		rep(i, v.size()) for(int j = n + m; j >= 0; j--) if(dp[i][j]){
			dp[i][j] = min(dp[i][j], 100);
			if(j + v[i].first <= n + m) dp[i + 1][j + v[i].first] += dp[i][j], prev[i + 1][j + v[i].first] = 0;
			if(j + v[i].second<= n + m) dp[i + 1][j + v[i].second]+= dp[i][j], prev[i + 1][j + v[i].second]= 1;
		}
		p = v.size() - 1;
		if(dp[p + 1][n] != 1){
			cout << "no" << endl;
			goto END;
		}
		while(p >= 0){
			if(prev[p + 1][n]) rep(j, ns[p].size()) ans.pb(ns[p][j]);
			else rep(j, ys[p].size()) ans.pb(ys[p][j]);
			n -= prev[p + 1][n] ? ns[p].size() : ys[p].size();
			p--;
		}
		sort(all(ans));
		rep(i, ans.size()) cout << ans[i] + 1 << endl;
		cout << "end" << endl;
		END:;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3084
final_rank: 2