True Liars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16338    Accepted Submission(s): 5724

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3038

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

题意:

给出p1个好人,p2个坏人,这里面好人只说真话,坏人只说假话。

然后会有回答x y yes/no,代表的意思是x说y是好人/坏人。

最后就问你能否通过这些回答判断出哪些是好人(个数等于p1)并且输出。

题解:

这题我当时只是把思路想出来了,最后代码的实现并没有独立完成,主要是代码的后半部分...

我们先分析题目,假定x说y是好人,那么现在x有两种情况(好/坏),根据这两种情况也可以确定出y的好坏;同理,如果x说y是坏人,也有两种情况。通过对这两种情况的分析,我们会发现,当x说y是好人是,他们是同类的;当x说y是坏人时,他们不是同类的。

根据这个我们可以想到带权的并查集,用数组v[x]代表x与其父节点的关系,当v[x]为0时x与其父亲同类,为1时不同类。由于这是一个环状的关系,所以会模2。

假定我们已经分好了类,那么就会有n个集合,每个集合有与父节点同类的,也有不同类的。

如果我们现在要确定出好人的数量,那么在每个集合里面只能选一种,这时就用dp来处理:设dp[i,j]的含义是处理到第i个集合时,和为j的方案总数。

那么初始化dp[0,0]=1,转移方程为dp[i,j]+=dp[i-1,j-k0,k1],k0,k1为i集合中的两类的数量。

最后输出路径的时候有许多种方法,有兴趣的可以看下其它的代码~

最后注意,如果p1等于0,也会输出一个end。我就是在这里被坑了好久。

具体代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int N = ;
int n,p1,p2,cnt;
int f[N],dp[N][N],v[N],g[N],set[N][]; int find(int x){
if(f[x]==x) return x;
int tmp = f[x];
f[x]=find(f[x]);
v[x]=(v[x]+v[tmp])%;
return f[x];
} int main(){
while(~scanf("%d%d%d",&n,&p1,&p2)){
if(!n && !p1 &&!p2) break;
cnt = ;memset(set,,sizeof(set));memset(dp,,sizeof(dp));
memset(f,-,sizeof(f));
for(int i=;i<=p1+p2;i++) f[i]=i,v[i]=;
char s[];int x,y;
for(int k=;k<=n;k++){
scanf("%d%d %s",&x,&y,s);
int fx=find(x),fy=find(y);
if(fx==fy) continue ;
f[fx]=fy;
if(s[]=='y') v[fx]=(v[x]+v[y])%;
else v[fx]=(v[x]+v[y]+)%;
}
for(int i=;i<=p1+p2;i++){
if(find(i)==i) g[i]=++cnt;
}
for(int i=;i<=p1+p2;i++) set[g[find(i)]][v[i]]++;//set数组记录第几组两类的个数
dp[][]=;//dp[i,j]前i个集合,和为j的情况数量
for(int i=;i<=cnt;i++){
for(int j=;j<=p1;j++){ //注意p1等于0的情况
if(j>=set[i][]) dp[i][j]+=dp[i-][j-set[i][]];
if(j>=set[i][]) dp[i][j]+=dp[i-][j-set[i][]];
}
}
int tmp = p1;
int choose[N];
memset(choose,-,sizeof(choose));
if(dp[cnt][p1]==){
for(int i=cnt;i>=;i--){
if(dp[i-][tmp-set[i][]]==dp[i][tmp]){
choose[i]=;
tmp-=set[i][];
}else if(dp[i-][tmp-set[i][]]==dp[i][tmp]){
choose[i]=;
tmp-=set[i][];
}
}
for(int i=;i<=p1+p2;i++){
if(choose[g[find(i)]]==v[i]) printf("%d\n",i);
}
puts("end");
}else puts("no");
}
return ;
}
/*
2 0 2
1 2 yes
2 1 yes
*/

POJ1417:True Liars(DP+带权并查集)的更多相关文章

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

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

  2. 【bzoj1202】[HNOI2005]狡猾的商人 带权并查集

    题目描述 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 Ai大于0时表 ...

  3. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

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

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

  5. 【11.1校内测试】【快速幂DP】【带权并查集】【模拟】

    Solution $jzy$大佬用了给的原根的信息,加上矩阵快速幂150行QAQ 然而$yuli$大佬的做法不仅好懂,代码只有50行! 快速幂的思想,把m看成要组成的区间总长度,每次将两段组合得到新的 ...

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

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

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

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

  8. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

  9. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

随机推荐

  1. python七类之字典详解

    一.字典 一.关键字:dict 1.字典是唯一的键值对数据,其表现形式:   dic  =  {键:值},​字典里的键必须保证是唯一的 2.键必须是不可变的数据类型: ​a.故列表是不能当键的 b.所 ...

  2. java基础 -- Collections.sort的两种用法

    /** * @author * @version * 类说明 */ package com.jabberchina.test; import java.util.ArrayList; import j ...

  3. 利尔达NB-IOT模块对接移动onenet平台步骤

    1. 首先登陆浙江移动onenet网站,http://openiot.zj.chinamobile.com/,进入右上角的开发者中心,然后才能看到创建产品 2. 填写产品的信息,其他信息按照个人实际填 ...

  4. ArcPy:GeoJSON转ArcGIS Geometry

    import arcpy geojson = {"type":"Polygon","coordinates":[[[120.81878662 ...

  5. IDEA的terminal设置成Linux的终端一样

    方式一:通过在Windows上安装Linux命令行工具 前提:需要安装Linux终端的命令行工具,并且最好可以安装 Gow (一个Windows下模拟Linux命令行工具集合,它集成了 Liunx 环 ...

  6. 修改有数据oracle字段类型 从number转为varchar

    --修改有数据oracle字段类型 从number转为varchar--例:修改ta_sp_org_invoice表中RESCUE_PHONE字段类型,从number转为varchar --step1 ...

  7. (原) MatEditor部- UmateriaEditor中Texture使用过程(1)

    @author: 白袍小道 转载说明原处 插件同步在GITHUB: DaoZhang_XDZ     最后YY需求(手滑)(开黑前弄下,充数,见谅) 1.在理清楚基础套路和细节后,自定义纹理资源,并加 ...

  8. [USACO18DEC]Fine Dining

    题面 \(Solution:\) 一开始想的是先跑一遍最短路,然后拆点之后再跑一遍,比较两次dis,然后发现拆点后会有负环(可能是我没想对拆点的方法),于是就放弃了拆点法. 我们考虑强制让每头牛选择走 ...

  9. 论文翻译 - Multiagent Bidirectionally-Coordinated Nets Emergence of Human-level Coordination in Learning to Play StarCraft Combat Games

    (缺少一些公式的图或者效果图,评论区有惊喜) (个人学习这篇论文时进行的翻译[谷歌翻译,你懂的],如有侵权等,请告知) Multiagent Bidirectionally-Coordinated N ...

  10. LeetCode 622——设计循环队列

    1. 题目 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列 ...