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 ...
随机推荐
- iOS 事件处理机制与图像渲染过程(转)
iOS 事件处理机制与图像渲染过程 iOS RunLoop都干了什么 iOS 为什么必须在主线程中操作UI 事件响应 CALayer CADisplayLink 和 NSTimer iOS 渲染过程 ...
- 段落排版--缩进(text-indent)
中文文字中的段前习惯空两个文字的空白,这个特殊的样式可以用下面代码来实现: p{text-indent:2em;} <p>1922年的春天,一个想要成名名叫尼克卡拉威(托比?马奎尔Tobe ...
- system.getProperties()
Properties props=System.getProperties(); //系统属性 System.out.println("Java的运行环境版本:"+prop ...
- Linux嘚瑟一时的Shared Object
场景概述 近来接触node程序以及负责实现node扩展来对象本地SDK的调用,旨在借node及其第三方库来快速实现RESTful API以及给浏览器端使用.当然这中间研究工作耗了不少时间. 在实现目标 ...
- 执行*.sh脚本时提示Permission denied
使用chmod修改.sh的权限 chmod u+x *.sh 再次执行
- 18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...
- mysql,left join on
转自http://www.oschina.net/question/89964_65912 觉得很有帮助,用来学习. 即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇 ...
- img超出div width时, jQuery动态改变图片显示大小
参考: 1. http://blog.csdn.net/roman_yu/article/details/6641911 2. http://www.cnblogs.com/zyzlywq/archi ...
- JS 获取各个宽度和高度
IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...
- Ruby自学笔记(五)— 条件判断
条件判断,在编程语言中都存在,而Ruby中的条件判断和Java中类似,当然还是存在些许差别 Ruby中条件判断的条件: 1) 可以使用 ==,<,>等比较运算来作为条件,比较运算可以返回t ...