题目链接:http://poj.org/problem?id=1417

Time Limit: 1000MS Memory Limit: 10000K

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

题意:

有一个人流落荒岛,岛上有好人阵营和坏人阵营,但是他没有办法从外表进行分别;

然后他决定问岛上的人X一个问题,问Y是不是好人;

好人阵营的人必须说真话,坏人阵营必须说谎话;

现在他只知道岛上好人阵营有p1个人,坏人阵营有p2个人,想要问n个问题,期望能确定每个人是否为好人……

若不能,则输出no;若能,则输出输出好人阵营的所有人的编号。

题解:

问X这个人Y是不是好人……

  若得到的回答是NO:假设X是好人,则Y是坏人;假设X是坏人,则Y是好人。说明X和Y必然是敌对阵营的。

  相反的,若得到的是YES答案,则X和Y是同一阵营的。

用并查集建树,每个节点有两个参数,par[x]和val[x],par[x]表示x的父亲节点的编号,val[x]表示x与父亲节点是不是同一阵营;

val[x]为0表示该节点与其父亲节点是同阵营,val[x]为1表示该节点与其父亲节点是敌对阵营。

则用并查集对n个问题进行建树处理完之后,就将p1+p2个人分为k棵树(k个集合),每个集合上有a个人是一个阵营,b个是另一阵营,(但不确定哪个是好人阵营,哪个是坏人阵营);

现在我们要做的就是构建一个方案:对k个集合,每个集合里选择a或b,认为它是这个集合内好人的人数,然后全部加起来正好等于p1;

若有且仅有这样的一个方案,那么就能精确判断那些人是好人阵营的,否则就只能输出no了。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=++; int n,p1,p2; int par[maxn];
bool val[maxn];
void init(int l,int r)
{
for(int i=l;i<=r;i++) par[i]=i,val[i]=;
} int find(int x)
{
if(par[x]==x) return x;
else //下面三步非常关键,顺序不能颠倒,要理解val[x]跟随par[x]变动的意义
{
int tmp=find(par[x]);
val[x]=val[x]^val[par[x]];
return par[x]=tmp;
}
} struct Tree{
int a,b;
vector<int> anode;
vector<int> bnode;
}tree[maxn];
bool treeRootAppeared[maxn];
int treeID[maxn]; struct DP{
int val;
int pre;
}dp[maxn][maxn]; int main()
{
while(scanf("%d%d%d",&n,&p1,&p2) && n+p1+p2!=)
{
init(,p1+p2);
for(int i=,a,b;i<=n;i++)
{
char tmp[];
scanf("%d%d%s",&a,&b,tmp); int t1=find(a),t2=find(b);
if(t1!=t2)
{
par[t1]=t2;
if(tmp[]=='n') val[t1]=!(val[a]^val[b]); //a,b两者是敌对阵营
else val[t1]=val[a]^val[b]; //a,b两者是同阵营
}
} //对每个集合内的两个阵营人数进行统计
for(int i=;i<=p1+p2;i++) //初始化每个集合
{
tree[i].a=tree[i].b=;
tree[i].anode.clear();
tree[i].bnode.clear();
}
int cnt=;
memset(treeRootAppeared,,sizeof(treeRootAppeared));
for(int i=;i<=p1+p2;i++)
{
int t=find(i); if(!treeRootAppeared[t]) //这个集合的树根还未出现过
{
treeRootAppeared[t]=;
if(val[i]==)
{
tree[cnt].a++;
tree[cnt].anode.push_back(i);
}
else
{
tree[cnt].b++;
tree[cnt].bnode.push_back(i);
}
treeID[i]=treeID[t]=cnt;
cnt++;
}
else //这个集合的树根出现过了
{
if(val[i]==)
{
tree[treeID[t]].a++;
tree[treeID[t]].anode.push_back(i);
}
else
{
tree[treeID[t]].b++;
tree[treeID[t]].bnode.push_back(i);
}
treeID[i]=treeID[t];
}
} //动态规划求方案数
memset(dp,,sizeof(dp));
dp[][].val=;
for(int i=;i<cnt;i++)
{
for(int j=;j<=p1;j++)
{
if(j>=tree[i].a && dp[i-][j-tree[i].a].val>)
{
dp[i][j].val+=dp[i-][j-tree[i].a].val;
dp[i][j].pre=j-tree[i].a;
}
if(j>=tree[i].b && dp[i-][j-tree[i].b].val>)
{
dp[i][j].val+=dp[i-][j-tree[i].b].val;
dp[i][j].pre=j-tree[i].b;
}
}
} if(dp[cnt-][p1].val!=) printf("no\n");
else
{
vector<int> ans;
for(int i=cnt-,j=p1; i>=; j=dp[i][j].pre,i--)
{
int tmp=j-dp[i][j].pre; //tmp为第i个集合内好人的人数
if(tmp==tree[i].a)
for(int k=;k<tree[i].anode.size();k++) ans.push_back(tree[i].anode[k]);
else
for(int k=;k<tree[i].bnode.size();k++) ans.push_back(tree[i].bnode[k]);
} sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++) printf("%d\n",ans[i]);
printf("end\n");
}
}
}

POJ 1417 - True Liars - [带权并查集+DP]的更多相关文章

  1. F - True Liars 带权并查集

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

  2. POJ 1733 Parity game (带权并查集)

    题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...

  3. POJ 1773 Parity game 带权并查集

    分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...

  4. POJ 1182 食物链 【带权并查集】

    <题目链接> 题目大意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我 ...

  5. POJ 1182 食物链 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78551   Accepted: 23406 Description ...

  6. POJ 1182 食物链 【带权并查集/补集法】

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...

  7. poj 1182 食物链【带权并查集】

    设相等的边权为0,吃的边权为,被吃的边权为2,然后用带权并查集在%3的意义下做加法即可 关系为简单环的基本都可以用模环长的方式是用带权并查集 #include<iostream> #inc ...

  8. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  9. POJ 3228 Gold Transportation(带权并查集,好题)

    参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...

随机推荐

  1. HTTPS原理,以及加密、解密的原理。

    https://blog.csdn.net/Yang_yangyang/article/details/79702583 摘要:本文用图文的形式一步步还原HTTPS的设计过程,进而深入了解原理. A在 ...

  2. 《倾国倾城》全套源代码:client+服务端+资源,歧视复制帖子

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  3. vuejs使用FormData对象,ajax上传图片文件

    我相信很多使用vuejs的朋友,都有采用ajax上传图片的需求,因为前后端分离后,我们希望都能用ajax来解决数据问题,传统的表单提交会导致提交成功后页面跳转,而使用ajax能够无刷新上传图片等文件. ...

  4. 【代码审计】TuziCMS_v3.0_任意文件删除漏洞分析

      0x00 环境准备 TuziCMS官网:http://www.tuzicms.com/ 网站源码版本:TuziCMS_v3.0_20161220 程序源码下载:http://www.tuzicms ...

  5. 【RF库Collections测试】lists should be equal

    场景一:msg=None 场景二:自定义msg 场景三:自定义msg和values,且values为布尔类型False或者字符串False和No Values 场景四:自定义msg和values,且v ...

  6. Robot Framework进行web ui自动化测试,浏览器配置说明

    转载请注明出处,谢谢: chrome浏览器: 1.从如下地址下载与本地浏览器版本号一致的chromedriver.exe驱动文件: http://chromedriver.storage.google ...

  7. Win8交互UX——键盘交互

    设计用户可以通过硬件键盘.屏幕键盘或触摸键盘交互的 Windows 应用商店应用. 本主题介绍键盘交互的设计注意事项.有关实现键盘交互的信息,请参阅响应键盘输入. 键盘交互 键盘输入是 Windows ...

  8. 【大数据系列】MapReduce详解

    MapReduce是hadoop中的一个计算框架,用来处理大数据.所谓大数据处理,即以价值为导向,对大数据加工,挖掘和优化等各种处理. MapReduce擅长处理大数据,这是由MapReduce的设计 ...

  9. 【大数据系列】hadoop单机模式安装

    一.添加用户和用户组 adduser hadoop 将hadoop用户添加进sudo用户组 sudo usermod -G sudo hadoop 或者 visudo 二.安装jdk 具体操作参考:c ...

  10. css笔记 - animation学习笔记(二)

    animation动画 @keyframes规则 - 创建动画 from - to 等价于 0% - 100% 但是优先使用0% - 100%,因为浏览器兼容性还好点 animation 动画绑定 将 ...