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 them 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 informationf 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 p1 p2 
xl yl a1 
x2 y2 a2 
... 
xi yi ai 
... 
xn yn an

The first line has three non-negative integers n, p1, and p2. n is the number of questions Akira asked. pl and p2 are the populations of the divine and devilish tribes, respectively, in the legend. Each of the following n lines has two integers xi, yi and one word ai. xi and yi are the identification numbers of inhabitants, each of which is between 1 and p1 + p2, inclusive. ai is either yes, if the inhabitant xi said that the inhabitant yi was a member of the divine tribe, or no, otherwise. Note that xi and yi 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 p1 and p2 are less than 300. A line with three zeros, i.e., 0 0 0, represents the end of the input. You can assume that each data set is consistent and no contradictory answers are included.

Output

For each data set, if it includes sufficient information to classify all the inhabitants, print the identification numbers of all the divine ones in ascending order, one in a line. In addition, following the output numbers, print end in a line. Otherwise, i.e., if a given data set does not include sufficient information to identify all the divine members, print no in a line.

Sample Input

2 1 1
1 2 no
2 1 no
3 2 1
1 1 yes
2 2 yes
3 3 yes
2 2 1
1 2 yes
2 3 no
5 4 3
1 2 yes
1 3 no
4 5 yes
5 6 yes
6 7 no
0 0 0

Sample Output

no
no
1
2
end
3
4
5
6
end
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 620
#define INF 0x3f3f3f3f
/*
并查集
如何DP dp[i][j] 代表前i个集合有j个好人方案数目
dp[i][j] = dp[i-1][j-same[i-1]]+1;
dp[i][j] = dp[i-1][j-other[i-1]]+1;
*/
using namespace std;
int pre[MAXN],same[MAXN],other[MAXN],rank[MAXN],dp[MAXN][MAXN];
bool been[MAXN];
int n,d,p1,p2;
int find(int x)
{
if(pre[x]==x) return x;
int fx = pre[x];
pre[x] = find(fx);
rank[x]= (rank[x]+rank[fx])%;
return pre[x];
}
bool mix(int x,int y,int d)
{
int fx=find(x),fy=find(y);
if(fx==fy)
{
if(rank[y]!=rank[x]+d)
return false;
return true;
}
pre[fy] = fx;
rank[fy] = rank[x]-rank[y]+d;
rank[fy] %=;
return true;
}
int main()
{
while(scanf("%d%d%d",&n,&p1,&p2),n+p1+p2)
{
string str;
int x,y,d;
for(int i=;i<=p1+p2;i++)
{
pre[i] = i;
rank[i] = same[i] = other[i] =;
}
for(int i=;i<n;i++)
{
cin>>x>>y>>str;
if(str[]=='y')
d=;
else
d=;
mix(x,y,d);
}
for(int i=;i<=p1+p2;i++)
{
int tmp = find(i);
for(int j=i;j<=p1+p2;j++)
{
int fj = find(j);
if(!been[j]&&fj==tmp)
{
been[j] = true;
same[fj]+=-rank[j];
other[fj]+=rank[j];
}
}
}
int cnt = ;
for(int i=;i<=p1+p2;i++)
{
if(other[i]>||same[i]>)
{
other[cnt] = other[i];
same[cnt++] = same[i];
}
}
dp[][] = ;
for(int i=;i<=cnt;i++)
{
for(int j=;j<=p1;j++)
{
if(j-same[i-]>=&&dp[i-][j-same[i-]])
dp[i][j] += dp[i-][j-same[i-]];
if(j-other[i-]>=&&dp[i-][j-other[i-]])
dp[i][j] += dp[i-][j-other[i-]];
}
}
if(dp[cnt][p1]==||dp[cnt][p1]>)
cout<<"no\n"; }
return ;
}

F - True Liars 带权并查集的更多相关文章

  1. POJ 1417 - True Liars - [带权并查集+DP]

    题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...

  2. 【bzoj3376-方块游戏】带权并查集

    题意: n块积木,m个操作或询问.每次移动积木的时候,约翰会选择两块积木X,Y,把X搬到Y的上方.如果X已经和其它积木叠在一起了,那么应将这叠积木整体移动到Y的上方:如果Y已经和其它积木叠在一起了的, ...

  3. 带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)

    F. Palindrome Problem Description A string is palindrome if it can be read the same way in either di ...

  4. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  5. BZOJ 1202: [HNOI2005]狡猾的商人 [带权并查集]

    题意: 给出m个区间和,询问是否有区间和和之前给出的矛盾 NOIp之前做过hdu3038..... 带权并查集维护到根的权值和,向左合并 #include <iostream> #incl ...

  6. luogu 2294 狡猾的商人 带权并查集

    此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...

  7. poj2492(带权并查集)

    题目链接:http://poj.org/problem?id=2492 题意:给出n个人,m条关系,每条关系表示的两个人异性,判断这m条关系是否有误. 思路:带权并查集,类似poj1182,并查集的向 ...

  8. BZOJ 1202 狡猾的商人 差分约束or带权并查集

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1202 题目大意: 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的 ...

  9. Bzoj1202/洛谷P2294 [HNOI2005]狡猾的商人(带权并查集/差分约束系统)

    题面 Bzoj 洛谷 题解 考虑带权并查集,设\(f[i]\)表示\(i\)的父亲(\(\forall f[i]<i\)),\(sum[i]\)表示\(\sum\limits_{j=fa[i]} ...

随机推荐

  1. codevs 2541 幂运算(迭代加深搜索)

    2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 从m开始,我们只需要6次运算就可以计算出m31 ...

  2. Akka源码分析-官方文档说明

    如果有小伙伴在看官方文档的时候,发现有些自相矛盾的地方,不要怀疑,可能是官方文档写错了或写的不清楚,毕竟它只能是把大部分情况描述清楚.开源代码一直在更新,官方文档有没有更新就不知道了,特别是那些官方不 ...

  3. 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)

    题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...

  4. MVC系列学习(十四)-路由规则及路由调试工具

    1.本次学习的代码,比较简单,就是在路由配置文件中,添加一个路由信息:同时添加一个相应的控制器及视图 控制器中代码如下 即有两条路由匹配规则,一个Kim控制器,该控制器下有个Index的方法,和一个对 ...

  5. Laravel5.1学习笔记17 数据库3 数据迁移

    介绍 建立迁移文件 迁移文件结构 执行迁移 回滚迁移 填写迁移文件  创建表 重命名/ 删除表 创建字段 修改字段 删除字段 建立索引 删除索引 外键约束 #介绍 Migrations are lik ...

  6. webstorm进行VisualSVN配置及上传项目到项目库

    以前建站一直都是自己一个人,最近要做一个比较大的网站,寻思着利用svn在整个开发过程中会比较快,于是摸索着配置了一下. 首先,下载VisualSVN这个软件,官网链接 https://www.visu ...

  7. java攻城狮之路--复习xml&dom_pull编程

    xml&dom_pull编程: 1.去掉欢迎弹窗界面:在window项的preferences选项中输入“configuration center” 找到这一项然后     把复选框勾去即可. ...

  8. 检索COM类工厂中CLSID 为 {000209FF-0000-0000-C000-000000000046}的组件时失败, 原因是出现以下错误: 80070005

    主要问题原因是Word权限配置问题 解决方案: 控制面板-管理工具-组件服务-计算机-我的电脑-DCOM配置 在列表中找到microsoft word97-2003 document 右键选择属性,选 ...

  9. PHP 之ftp客户端类封装实现

    <?php /** * Class FtpClient */ class FtpClient { private $host = '';//远程服务器地址 private $user = ''; ...

  10. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...