传送门

Description

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

首先我们可以根据这些问话进行合并,yes表示同类人,no表示不同类,所以我们可以将这些人分成一定的集合

然后可以通过dp来找到是否有唯一解

AC代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
const int MAXN=610;
int f[1010],sum[1010];
int a[MAXN][2];
vector<int>b[MAXN][2];
bool used[MAXN];
int dp[MAXN][MAXN/2];
int pre[MAXN][MAXN/2];
int find(int x) {if(f[x]!=x) {int i=f[x];f[x]=find(f[x]);sum[x]=(sum[x]+sum[i])%2;}return f[x];
}
int main() {int n,p1,p2;while(cin>>n>>p1>>p2,n+p1+p2) {for(int i=0;i<=p1+p2;i++)f[i]=i,sum[i]=0;for(int i=1; i<=n; i++) {int a,b,k;string s;cin>>a>>b>>s;int aa=find(a),bb=find(b);if(s=="yes") k=0;else k=1;if(aa!=bb) f[aa]=bb, sum[aa]=(sum[b]-sum[a]+k+2)%2;}for(int i=0; i<MAXN; i++) {b[i][0].clear();b[i][1].clear();a[i][0]=0;a[i][1]=0;}memset(used,false,sizeof(used));int cnt=1;for(int i=1; i<=p1+p2; i++)if(!used[i]) {int tmp=find(i);for(int j=i; j<=p1+p2; j++) {if(find(j)==tmp) {used[j]=true;b[cnt][sum[j]].push_back(j);a[cnt][sum[j]]++;}}cnt++;}memset(dp,0,sizeof(dp));dp[0][0]=1;for(int i=1; i<cnt; i++) {for(int j=p1; j>=0; j--) {if(j-a[i][0]>=0 && dp[i-1][j-a[i][0]]) {dp[i][j]+=dp[i-1][j-a[i][0]];pre[i][j]=j-a[i][0];}if(j-a[i][1]>=0 && dp[i-1][j-a[i][1]]) {dp[i][j]+=dp[i-1][j-a[i][1]];pre[i][j]=j-a[i][1];}}}if(dp[cnt-1][p1]!=1)     printf("no\n"); else {vector<int>ans;ans.clear();int t=p1;for(int i=cnt-1; i>=1; i--) {int tmp=t-pre[i][t];if(tmp==a[i][0]) {for(int j=0; j<a[i][0]; j++)ans.push_back(b[i][0][j]);} else {for(int j=0; j<a[i][1]; j++)ans.push_back(b[i][1][j]);}t=pre[i][t];}sort(ans.begin(),ans.end());for(int i=0; i<ans.size(); i++)printf("%d\n",ans[i]);printf("end\n");}}return 0;
}

POJ 1417 True Liars(带权并查集+DP)相关推荐

  1. POJ 1417 True Liars 带权并查集 + 背包

    一.内容 After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally ...

  2. POJ 1417 True Liars(路径压缩并查集+DP背包问题)

    POJ 1417 True Liars(路径压缩并查集+DP背包问题) http://poj.org/problem?id=1417 题意: 给出p1+p2个人,其中p1个是好人,p2个是坏人.然后有 ...

  3. POJ - 1417 True Liars POJ - 141 带权并查集,01背包问题

    题目链接 POJ-1417 题意 岛上有说真话的好人和说假话的坏人,给你这两种人的人数.再给出q次问答结果,问答的格式是向a询问b是否是好人,回答是yes或者no.问是否可以分辨出全部好人,是的话打印 ...

  4. POJ 1417 True Liars 并查集+背包

    题目链接:http://poj.org/problem?id=1417 解题思路:比较容易想到的是并查集,然后把第三组数据测试一下之后发现这并不是简单的并查集,而是需要合并之后然后判断的.并且鉴于题目 ...

  5. 【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)

    题干: Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36176   Accep ...

  6. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  8. POJ 2492 A Bug's Life 带权并查集

    题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...

  9. POJ 2912 Rochambeau(难,好题,枚举+带权并查集)

    下面的是从该网站上copy过来的,稍微改了一点,给出链接:http://hi.baidu.com/nondes/item/26dd0f1a02b1e0ef5f53b1c7 题意:有N个人玩剪刀石头布, ...

最新文章

  1. 薛澜:人工智能发展要让创新驱动和敏捷治理并驾齐驱
  2. mysql命令行安装报错_centos命令行安装mysql随机密码查看方法(遇到问题及其解决办法)...
  3. spark sql 查看分区_Spark 3.0 中七个必须知道的 SQL 性能优化
  4. 第二次作业——个人项目实战
  5. 10.2 运算符重载函数作为类成员函数和友元函数
  6. 加速数据读取的利器-缓存及分布式存储
  7. 深度学习入门(一):LeNet-5教程与详解
  8. Fabric 1.0的多机部署
  9. php处理ubb代码,UBB代码详解
  10. 闲置eSATA接口影响Windows7启动速度案例分析
  11. 给英文文章加音标,建生词表
  12. java操作cad_java调用AutoCAD绘图
  13. aar64不支持Pycharm部分版本导致cannot open local terminal的解决方法
  14. 记录一下3dmax--substancePainter-unity烘焙法线详细过程
  15. 怎么实现EDIUS中添加的图片素材一样大
  16. 32岁医生放弃医院编制,转行去做程序员!
  17. OrientedRepPoints_DOTA环境搭建训练流程及问题
  18. everything_常用搜索语法/目录搜索(by offical doc/my examples)
  19. CRC32算法理论和分库分表业务实现
  20. 企业生产发展从第一次工业革命到信息化时代

热门文章

  1. 输入一个字符串并原样输出。
  2. 这几个网站的使用技巧,值得反复读,反复练~
  3. 【ES知识】ES基础查询语法一览
  4. 阿里云ECS共享型、通用型和突发型实例类型有什么区别?如何选择?
  5. CGAN(条件生成-对抗网络)简述教程
  6. [Android]混淆Android代码
  7. Import Netscaler VPX10.5 to Hyper-V 2012R2
  8. matlab复杂噪声产生实验报告,基于小波信号的噪声消除matlab实验报告.docx
  9. php distinct 用法,distinct的用法
  10. Qt: Linux下生成.xlsx文件(excel表格文件)