一、内容

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 anThe 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 <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 605;
int m, n, n1, n2, x, y, p[N], d[N], dp[N][N], w[N][2], vis[N];
char s[5];
map<int, int> mp; //代表每个连通块的编号
int find(int x) {if (p[x] == x) return x;int root = find(p[x]);d[x] = (d[x] + d[p[x]]) % 2; //更新到根节点的距离 2种状态 0与根节点同类 1与根节点非同类 p[x] = root;return p[x];
}
int main() {while (true) {scanf("%d%d%d", &m, &n1, &n2);if (!m && !n1 && !n2) break;n = 0; mp.clear(); memset(w, 0, sizeof(w));memset(dp, 0, sizeof(dp));memset(vis, -1, sizeof(vis));//代表某个选没有选 选的是什么类型的 for (int i = 1; i <= n1 + n2; i++) p[i] = i, d[i] = 0;for (int i = 1; i <= m; i++) {scanf("%d%d%s", &x, &y, s);int fx = find(x), fy = find(y);if (fx == fy) continue; if (s[0] == 'y') {p[fx] = fy; d[fx] = d[y] + d[x]; //让其变成正数 } else {p[fx] = fy; d[fx] = d[y] + d[x] + 1; }} //将每个连通块进行打包 w[][] 0里面放与根节点同类 1放与根节点不同类 for (int i = 1; i <= n1 + n2; i++) {int f = find(i);if (!mp.count(f)) mp[f] = ++n;w[mp[f]][d[i]]++; //d[i]代表状态 }//进行背包的选择dp[0][0] = 1;for (int i = 1; i <= n; i++) {for (int j = 0; j <= n1; j++) {if (w[i][0] <= j) dp[i][j] += dp[i - 1][j - w[i][0]];if (w[i][1] <= j) dp[i][j] += dp[i - 1][j - w[i][1]];             }} if (dp[n][n1] == 1) {//代表只有一种方案 int x = n, y = n1;while (x != 0 && y != 0) {if (dp[x][y] == dp[x - 1][y - w[x][0]]) {vis[x] = 0;//选了0这个类型y -= w[x][0]; } else if (dp[x][y] == dp[x - 1][y - w[x][1]]) {vis[x] = 1; y -= w[x][1];}x--;}for (int i = 1; i <= n1 + n2; i++) {int id = mp[p[i]]; //连通块编号 if (vis[id] == d[i]) printf("%d\n", i);}printf("end\n");} else printf("no\n");}return 0;
}

POJ 1417 True Liars 带权并查集 + 背包相关推荐

  1. POJ 1417 True Liars(带权并查集+DP)

    传送门 Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda ...

  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. 2021绵阳南山高考成绩查询,绵阳南山中学举行2021年冲刺高考百日誓师大会
  2. redis事务原理,使用,详解
  3. 【POJ - 1556】The Doors (计算几何,线段相交)
  4. 前端页面加水印插件_没用过这7款浏览器插件,你一定是假的程序员
  5. 辽宁计算机专业院校排名2015,liaoning高校排行榜_辽宁高校排名 2015年辽宁省最佳大学排行榜...
  6. QT Creator 使用 design 修改 ui界面编译后界面未更新代码提示
  7. [Python] L1-037. A除以B 团体程序设计天梯赛GPLT
  8. 计算机网络数据链路层之扩展以太网(含以太网交换机及虚拟局域网)
  9. Leetcode 刷题笔记(十七) —— 二叉树篇之公共祖先问题
  10. python github库_让pip使用git和github存储库
  11. 2018年6月2日 星期六 天气晴
  12. jQuery改变网页背景颜色切换的方法
  13. 已解决-NVIDIA安装程序失败-win10
  14. springboot 删除路径下面所有文件_[原创]springboot 中 resources 资源目录里面的文件夹压缩下载...
  15. 自带软件 测试二手电脑,二手电脑怎么检测配置
  16. 【JavaWeb】前置知识:CSS与JavaScript知识汇总
  17. matlab张志涌版课后习题答案,matlab教程(张志涌)课后习题答案.doc
  18. 互联网医院 2020年突出成就_我省2020年互联网企业20强榜单出炉
  19. lucas定理(学习笔记)
  20. JS与DOM的兼容性

热门文章

  1. 棋牌游戏算法——字牌系列总结
  2. C语言pow()函数:求x的y次方(次幂)
  3. 什么是深度学习?有哪些相关书籍推荐?
  4. Shell中declare -A的用法?
  5. mysql情况数据库表数据函数_mysql数据库表单查询和函数的使用
  6. 一个小型RISC-V开源处理器核介绍!
  7. 《构建之法》读书笔记——第1章 概论
  8. html模版i7,2017万元级专业制图 Intel最新七代i7-7700K专业建模渲染设计师电脑配置推荐...
  9. 动手深度学习v2 汇聚层pooling 课后习题
  10. 网易云歌单信息爬取及数据分析(1)爬虫部分