题意:有两种人,一种人只会说真话,另一种人只会说假话。只会说真话的人有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的更多相关文章

  1. POJ 1417 - True Liars - [带权并查集+DP]

    题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...

  2. poj 1417 True Liars(并查集+背包dp)

    题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...

  3. POJ 1417 True Liars(种类并查集+dp背包问题)

    题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...

  4. POJ1417 True Liars —— 并查集 + DP

    题目链接:http://poj.org/problem?id=1417 True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  5. True Liars POJ - 1417

    True Liars After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was f ...

  6. poj 1417(并查集+简单dp)

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2087   Accepted: 640 Descrip ...

  7. POJ1417 True Liars

    题意 Language:Default True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6392 Accep ...

  8. POJ1417:True Liars(DP+带权并查集)

    True Liars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. POJ 1417 并查集 dp

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

随机推荐

  1. SQL server 使用触发器跨数据库备份数据

    create database TriggerTest create table transInfo2 --交易信息表 ( cardID ) not null, --卡号 transType ) no ...

  2. PreferenceFragment界面透明问题

    PreferenceFragment界面默认是透明的 而其布局代码框架为 <PreferenceScreen> ... </PreferenceScreen>,背景色及透明度属 ...

  3. ASP.NET如何发布更新

    如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...

  4. CI 笔记(easyui js命令)

    1. 两种方式加载easyui,一是用class自动渲染,一种是js.建议js. 2. 参考李炎恢的easyui的视频教程.最好的一个视频,对于easyui.

  5. 初识sass框架

    编写过页面的开发者都知道css这个东西,究其原意,也就是层叠样式表,我们页面的三大结构,html css javascript,其中html负责主要的页面结构,css就负责主要的页面样式,而我们的js ...

  6. Direct 2D实现界面库 (1)

    大学时尝试过很多次写一个UI库, 初次使用 GDI 绘图, 当时水平很低, GDI功能太弱, 以失败而告终. 之后使用 GDI+ 绘图, 当时水平依旧很低, GDI功能很强, 但效率实在太慢, 以失败 ...

  7. MVC Unit Testing学习笔记

    MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...

  8. 固定DIV样式

      <!doctype html>   <html>   <head>   <meta charset="UTF-8">   < ...

  9. wechat-php-sdk

    wechat-php-sdk 微信公众平台php版开发包 支持消息加解密方式的明文模式.兼容模式.安全模式 支持自动接入微信公众平台(步骤) 功能模块 Wechat (处理自动接入.获取与回复微信消息 ...

  10. POJ1258-Agri-Net-ACM

    Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...