POJ 1417 True Liars
题意:有两种人,一种人只会说真话,另一种人只会说假话。只会说真话的人有p1个,另一种人有p2个。给出m个指令,每个指令为a b yes/no,意思是,如果为yes,a说b是只说真话的人,如果为no,a说b是只说假话的人。注意,a可以为b。保证每个指令都是正确的,且相互之间不矛盾。问,能不能确定哪些人是说真话的人,如果能,输出所有只说真话的人;如果不能,输出no。
解法:首先,用并查集建树,每个节点有两个参数,f和r,f表示父亲节点的编号,r表示与父亲节点是不是同一种人。r为0表示该节点与父亲节点是一种人,r为1表示该节点与父亲节点不是一种人。则用并查集处理之后,就可以分为k棵树,每棵树上可能有x[i]个人是同一种人,y[i]个是另一种人,(不确定哪个是说真话人的数量,哪个是说假话的人的数量),且每棵树之间互不影响。所以即是一个背包问题,用dp解决即可。
tag:并查集,DP,背包
/*
* Author: Plumrain
* Created Time: 2013-11-28 10:26
* File Name: DS-POJ-1417.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
typedef pair<int, int> pii; struct oo{
int a, b, pos;
void clr(){
a = ; b = ;
}
}; struct node{
int f, r;
}; node p[];
pii num[];
vector<int> ans;
oo cnt[];
map<int, int> mp;
int n, m, p1, p2, d[][]; int find(int x)
{
if (x != p[x].f){
int y = p[x].f;
p[x].f = find(p[x].f);
p[x].r = (p[x].r + p[y].r) % ;
}
return p[x].f;
} void merge(int a, int b, int x, int t1, int t2)
{
p[t1].f = t2;
p[t1].r = (p[a].r + p[b].r + x) % ;
} void init()
{
for (int i = ; i <= n; ++ i)
cnt[i].clr(); for (int i = ; i <= n; ++ i){
p[i].f = i;
p[i].r = ;
} int a, b, x;
char s[];
for (int i = ; i < m; ++ i){
scanf ("%d%d%s", &a, &b, s);
if (s[] == 'y') x = ;
else x = ; int t1 = find(a), t2 = find(b);
if (t1 != t2)
merge(a, b, x, t1, t2);
}
} void gao0()
{
if (p2 == || p1 == ){
for (int i = ; i <= p1; ++ i)
printf ("%d\n", i);
printf ("end\n");
return;
}
printf ("no\n");
} int main()
{
while (scanf ("%d%d%d", &m, &p1, &p2) != EOF){
if (!m && !p1 && !p2) break; if (!m){
gao0();
continue;
} n = p1 + p2;
init(); if (p1 == p2){
printf ("no\n");
continue;
} CLR (num);
for (int i = ; i <= n; ++ i){
int y = find(i);
if (!p[i].r) ++ num[y].first;
else ++ num[y].second;
} int all = ;
for (int i = ; i <= n; ++ i)
if (num[i].first + num[i].second){
cnt[all].a = num[i].first;
cnt[all].b = num[i].second;
cnt[all].pos = i;
++ all;
} CLR1 (d);
d[][] = ;
for (int i = ; i < all; ++ i)
for (int j = ; j <= p1; ++ j){
if (j >= cnt[i].a && d[i-][j-cnt[i].a] >= ){
if (d[i][j] == -) d[i][j] = ;
d[i][j] += d[i-][j-cnt[i].a];
}
if (j >= cnt[i].b && d[i-][j-cnt[i].b] >= ){
if (d[i][j] == -) d[i][j] = ;
d[i][j] += d[i-][j-cnt[i].b];
}
} if (d[all-][p1] != ) printf ("no\n");
else{
int j = p1;
mp.clear();
for (int i = all-; i; -- i){
if (j >= cnt[i].a && d[i-][j-cnt[i].a] == ){
j -= cnt[i].a;
mp[cnt[i].pos] = ;
}
else if (j >= cnt[i].b && d[i-][j-cnt[i].b] == ){
j -= cnt[i].b;
mp[cnt[i].pos] = ;
}
} ans.clear();
for (int i = ; i <= n; ++ i){
int y = find(i);
if (mp.count(y) && mp[y] == p[i].r)
ans.PB (i);
} sort(ans.begin(), ans.end());
int sz = ans.size();
for (int i = ; i < sz; ++ i)
printf ("%d\n", ans[i]);
printf ("end\n");
} }
return ;
}
POJ 1417 True Liars的更多相关文章
- POJ 1417 - True Liars - [带权并查集+DP]
题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...
- poj 1417 True Liars(并查集+背包dp)
题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...
- POJ 1417 True Liars(种类并查集+dp背包问题)
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
- POJ1417 True Liars —— 并查集 + DP
题目链接:http://poj.org/problem?id=1417 True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- True Liars POJ - 1417
True Liars After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was f ...
- poj 1417(并查集+简单dp)
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ...
- POJ1417 True Liars
题意 Language:Default True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6392 Accep ...
- POJ1417:True Liars(DP+带权并查集)
True Liars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 1417 并查集 dp
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
随机推荐
- Shell case正则匹配法
Shell case正则匹配法 case $BOOLEAN in [yY][eE][sS]) echo 'Thanks' $BOOLEAN ;; [yY]|[nN]) echo 'Thanks' ...
- 确认(confirm 消息对话框)
confirm 消息对话框通常用于允许用户做选择的动作(包括一个确定按钮和一个取消按钮). 语法: confirm(str) str:在消息对话框中要显示的文本 返回值: 当用户点击"确定& ...
- SGU 133.Border
水题不说了 #include <iostream> #include <cstring> #include <cstdio> #include <cmath& ...
- SGU 200.Cracking RSA(高斯消元)
时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...
- 了解负载均衡 会话保持 session同步(转)
一,什么负载均衡 一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情 ...
- 基于jQuery 的图片瀑布流实现
解题思路: 第1步 分析问题:我这边的处理方式是以列为单位.每次滚动条滚到底部,把需要加的新的内容放在高度最小的列.如下图所示 加载后的显示 如果在继续往下滚动.新图片就会在1下边显示,如此类推. ...
- CodeIgniter框架——介绍
CodeIgniter 是一个应用程序框架 CodeIgniter 是一个为用 PHP 编写网络应用程序的人员提供的工具包.它的目标是实现让你比从零开始编写代码更快速地开发项目,为此,CI 提供了一套 ...
- Oracle 面试宝典 - General Questions
转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...
- Ubuntu14.02 Sublimte2安装
$sudo add-apt-repository ppa:webupd8team/sublime-text-2 $sudo apt-get update $sudo apt-get install s ...
- NameNode元数据的管理机制(三)
元数据的管理: 第一步:客户端通过DistributedFilesystem 对象中的creat()方法来创建文件,此时,RPC会 通过一个RPC链接协议来调用namenode,并在命名空间中创建一个 ...