POJ 1417 True Liars 并查集+背包
题目链接:http://poj.org/problem?id=1417
解题思路:比较容易想到的是并查集,然后把第三组数据测试一下之后发现这并不是简单的并查集,而是需要合并之后然后判断的。并且鉴于题目要求输出数据,因此还要记录数据,可以说是非常有意思的题目。
首先,如果a b yes,那么a与b一定都是圣人或者恶人;反之,如果a b no,那么两者一个圣人,一个恶人。因此可以将所有元素分为若干个集合,每个集合中放两个小集合,表示两种不一样的人,当然并不知道到底哪个里面是圣人,恶人。
另一个比较棘手的问题就是集合区分的情况,可以使用带权并查集,val[i]记录每个元素与祖先的关系,0为同种人,1为不同种人。那么在合并的时候就需要计算:val[ra] = (val[b] - val[a] + tmp + 2) % 2; 其中ra,rb为a,b的祖先,tmp表示a,b关系是yes -- 0, no -- 1。另外就是并查集find路径压缩的时候要更新val值。
还有一个问题就是DP了,可以这样:dp[i][j]表示前i个集合恰好选j个人圣人的方案数目,pre[i][j]则用来记录在背包i - 1处选择的人数,dp[bgcnt][p1]不为1的话输出no。
状态转移方程:
1 dp[0][0] = 1; 2 for(int i = 1; i <= bgcnt; i++){ 3 for(int j = 0; j <= p1; j++){ 4 if(j >= bagcnt[i][0] && dp[i - 1][j - bagcnt[i][0]]){ 5 dp[i][j] += dp[i - 1][j - bagcnt[i][0]]; 6 pre[i][j] = j - bagcnt[i][0]; 7 } 8 if(j >= bagcnt[i][1] && dp[i - 1][j - bagcnt[i][1]]){ 9 dp[i][j] += dp[i - 1][j - bagcnt[i][1]]; 10 pre[i][j] = j - bagcnt[i][1]; 11 } 12 } 13 }
整体步骤就比较好说了,大致就是:并查集合并--枚举每个人放入对应的集合,同时统计大小集合的人数--动态规划统计方案数目,同时记录从大集合选择的人数--逆序求每次选择的小集合并保存其所有元素--排序输出
整体过程比较复杂,写了好长时间,需要耐着性子。
另外给出一些测试数据,选择poj discuss块,感谢各位,如有不妥,敬请指出。
1 6 4 4 2 1 3 yes 3 1 5 no 4 6 6 yes 5 5 7 no 6 2 8 no 7 2 4 yes 8 9 6 6 2 10 1 3 yes 11 1 5 no 12 6 6 yes 13 5 7 no 14 2 8 no 15 2 4 yes 16 17 6 5 3 18 1 3 yes 19 1 5 no 20 6 6 yes 21 5 7 no 22 2 8 no 23 2 4 yes 24 0 0 1 25 26 27 1 1 0 28 1 1 yes 29 30 3 2 0 31 1 2 no 32 2 2 yes 33 1 1 yes 34 35 3 2 0 36 1 1 yes 37 2 2 yes 38 1 1 yes 39 40 7 4 2 41 2 3 no 42 3 4 no 43 4 3 no 44 5 5 yes 45 5 3 no 46 5 5 yes 47 1 5 yes 48 49 11 9 7 50 6 12 no 51 4 8 yes 52 3 12 yes 53 5 9 no 54 6 11 yes 55 6 5 no 56 15 4 yes 57 16 15 yes 58 10 3 no 59 6 16 no 60 2 6 no 61 6 2 4 62 1 1 yes 63 2 3 yes 64 3 4 yes 65 4 5 no 66 5 6 yes 67 6 6 yes 68 3 3 2 69 1 2 no 70 3 4 yes 71 3 5 no 72 73 0 0 0 74 75 输出: 76 no 77 1 78 2 79 3 80 4 81 6 82 7 83 end 84 no 85 end 86 1 87 end 88 no 89 1 90 2 91 end 92 1 93 2 94 4 95 5 96 end 97 no 98 5 99 6 100 end 101 no
View Code
代码:
1 const int maxm = 700; 2 int bagcnt[maxm][2]; 3 vector<int> bagele[maxm][2]; 4 int bgcnt; 5 int dp[maxm][maxm], pre[maxm][maxm]; 6 int fa[maxm], val[maxm], fb[maxm]; 7 int vis[maxm]; 8 int n, p1, p2, xn; 9 vector<int> anss; 10 11 void init(){ 12 xn = p1 + p2; 13 bgcnt = 0; 14 for(int i = 0; i <= xn; i++) { 15 fa[i] = i; 16 bagele[i][0].clear(); 17 bagele[i][1].clear(); 18 } 19 memset(val, 0, sizeof(val)); 20 memset(fb, 0, sizeof(fb)); 21 memset(bagcnt, 0, sizeof(bagcnt)); 22 memset(vis, 0, sizeof(vis)); 23 memset(dp, 0, sizeof(dp)); 24 memset(pre, 0, sizeof(pre)); 25 } 26 int find(int x){ 27 if(x ==fa[x]) return x; 28 int tmp = find(fa[x]); 29 val[x] += val[fa[x]]; 30 val[x] %= 2; 31 return fa[x] = tmp; 32 } 33 34 int main(){ 35 while(scanf("%d %d %d", &n, &p1, &p2) && !(n == 0 && p1 == 0 && p2 == 0)){ 36 init(); 37 for(int i = 1; i <= n; i++){ 38 char str[5]; 39 int a, b; 40 scanf("%d %d %s", &a, &b, str); 41 int tmp = str[0] == 'y'? 0: 1; 42 int rb = find(b), ra = find(a); 43 if(ra != rb){ 44 fa[ra] = rb; 45 val[ra] = (val[b] - val[a] + tmp + 2) % 2; 46 } 47 } 48 if(p1 == p2){ 49 puts("no"); 50 continue; 51 } 52 for(int i = 1; i <= xn; i++){ 53 int ri = find(i); 54 if(fb[ri] == 0) fb[ri] = ++bgcnt; 55 bagele[fb[ri]][val[i]].push_back(i); 56 bagcnt[fb[ri]][val[i]]++; 57 } 58 59 // test 60 // puts("ancestors:"); 61 // for(int i = 1; i <= xn; i++) printf("%d:%d\n", i, fa[i]); 62 // for(int i = 1; i <= bgcnt; i++){ 63 // printf("bag %d:\ncnt0:%d cnt1:%d\n", i, bagcnt[i][0], bagcnt[i][1]); 64 // for(int j = 0; j < bagele[i][0].size(); j++) printf("%d ", bagele[i][0][j]); 65 // puts(""); 66 // for(int j = 0; j < bagele[i][1].size(); j++) printf("%d ", bagele[i][1][j]); 67 // puts(""); 68 // } 69 70 dp[0][0] = 1; 71 for(int i = 1; i <= bgcnt; i++){ 72 for(int j = 0; j <= p1; j++){ 73 if(j >= bagcnt[i][0] && dp[i - 1][j - bagcnt[i][0]]){ 74 dp[i][j] += dp[i - 1][j - bagcnt[i][0]]; 75 pre[i][j] = j - bagcnt[i][0]; 76 } 77 if(j >= bagcnt[i][1] && dp[i - 1][j - bagcnt[i][1]]){ 78 dp[i][j] += dp[i - 1][j - bagcnt[i][1]]; 79 pre[i][j] = j - bagcnt[i][1]; 80 } 81 } 82 } 83 // test 84 // printf("dp: %d\n", dp[bgcnt][p1]); 85 // int j = p1, tmp = p1; 86 // for(int i = bgcnt; i > 0; i--){ 87 // printf("pre %d %d: %d\n", i, j,tmp - pre[i][j]); 88 // tmp = pre[i][j]; 89 // j = pre[i][j]; 90 // } 91 if(dp[bgcnt][p1] != 1){ 92 puts("no"); 93 continue; 94 } 95 96 anss.clear(); 97 int tmp = p1, j = p1; 98 for(int i = bgcnt; i > 0; i--){ 99 int tmx = tmp - pre[i][j]; 100 if(bagcnt[i][0] == tmx){ 101 for(int k = 0; k < bagele[i][0].size(); k++) 102 anss.push_back(bagele[i][0][k]); 103 } 104 else{ 105 for(int k = 0; k < bagele[i][1].size(); k++) 106 anss.push_back(bagele[i][1][k]); 107 } 108 tmp = pre[i][j]; 109 j = pre[i][j]; 110 } 111 sort(anss.begin(), anss.end()); 112 for(int i = 0; i < anss.size(); i++){ 113 printf("%d\n", anss[i]); 114 } 115 puts("end"); 116 } 117 }
题目:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3677 | Accepted: 1186 |
Description
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
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
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
Source
转载于:https://www.cnblogs.com/bolderic/p/7298280.html
POJ 1417 True Liars 并查集+背包相关推荐
- POJ 1417 True Liars(路径压缩并查集+DP背包问题)
POJ 1417 True Liars(路径压缩并查集+DP背包问题) http://poj.org/problem?id=1417 题意: 给出p1+p2个人,其中p1个是好人,p2个是坏人.然后有 ...
- POJ 1417 True Liars (种类并查集+DP)
题目:True Liars 题目大意:给出n对关系,p1个好人,p2个坏人.要求根据n对关系中找出好人有哪些,若方案唯一,则逐个输出好人,最后输出end:若方案不唯一/找不到,那么输出no 结论:通过 ...
- POJ - 1417 True Liars POJ - 141 带权并查集,01背包问题
题目链接 POJ-1417 题意 岛上有说真话的好人和说假话的坏人,给你这两种人的人数.再给出q次问答结果,问答的格式是向a询问b是否是好人,回答是yes或者no.问是否可以分辨出全部好人,是的话打印 ...
- [POJ 1417] True Liars
[题目链接] http://poj.org/problem?id=1417 [算法] 首先,我们发现 : 如果A说B是好人,那么A和B是同一类人,否则A和B不是同一类人 利用这个 ...
- POJ - 1417(True Liars)
题意:雾岛上有两个部落,天使部落有 p1 个人,他们只说真话,恶魔部落有 p2 个人,他们只说假话,但是两个部落的人长得一模一样,把这 p1+p2 个人从1编号,然后有 n 次询问,每次询问给出 a ...
- POJ 1417 True Liars 带权并查集 + 背包
一.内容 After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally ...
- POJ 1417 True Liars(带权并查集+DP)
传送门 Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda ...
- POJ 2236 Wireless Network 并查集
Wireless Network 并查集 Crawling in process... Crawling failed Time Limit:10000MS Memory Limit:6553 ...
- POJ 1182 食物链,并查集的拓展
http://poj.org/problem?id=1182 /******************************************************** 此道题目 前天看的时候 ...
最新文章
- V210 系统时间设置
- 解决 java.net.ConnectException: Connection refused: connect 异常
- JavaScript实现斐波那契数列(Febonacci Array)
- 输入这是我的第一个python程序_「vs2017」vs2017 开始自己的第一个Python程序 - seo实验室...
- (数论)逆元的线性算法
- 20190720学习小结
- python中的time的时间戳_python中time、date、时间戳的转换
- web安全day35:Linux防火墙进阶
- 焕新:CANape 19真香~
- java fakepath_20140920遇到的问题--JAVA----JS------Tomcat7.0+Onselect灵敏度+fakepath等若干问题...
- 计算机积木游戏,儿童智力开发游戏:四款适合幼儿的积木游戏
- 房地产中介ERP管理系统设计方案(附源码)
- PHP 微信 消费者投诉 下载图片 api接口
- 电容的材质资料/什么cog,x5r y5v等知识
- 浅谈数码相机成像中 ISO(感光度)的作用
- 10005 内联函数
- 2D横版游戏角色素材可商用
- python创建网盘_超简单!基于Python搭建个人“云盘”
- python 内存释放
- RSD 教程 —— 4 框架
热门文章
- 《Spring参考手册》中定义了以下几个AOP的重要概念
- VS2008中开发Silverlight 2.0的配置
- 当当购书双十一钜惠,5折封顶!附图灵人工智能书单
- ICCV2021-PiT-池化操作不是CNN的专属,ViT说:“我也可以”;南大提出池化视觉Transformer(PiT)...
- 关于决策树,你一定要知道的知识点!
- 图匠数据等提出高精度零售货架姿态估计算法GSPN
- CVPR 2019 爆款论文作者现场解读:视觉语言导航、运动视频深度预测、6D姿态估计...
- PyTorch语义分割开源库semseg
- spark写入oracle 优化,spark读写数据库大表分区性能优化
- 人工智能学习书单推荐