True Liars

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.
 
 
思路:我们可以这么理解题目意思:有两个种族的人,他们说自己人是“yes”,说其他种族的人是“no”。
我们可以先根据自己人权值0,其他族人权值1来创建一颗权值线段树。这样,我们可能有若干颗树,分为两个种族。假设两个种族分别是x,y,因为不同树有两种种族人数,但无法分辨他们是哪个种族的人,题目需要明确其中一个种族人的编号,说明这个情况是唯一的,即树的人员分配出现p1,p2的情况是唯一的。我们可以先统计不同树上两个种族的人数,通过计数dp来实现。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath> using namespace std; #define ll long long
#define pb push_back
#define fi first
#define se second const int N = ;
struct node
{
int rt, v;
}fa[N];
int dp[N][N];
int a[N][];//不同树上两种人的人数
vector<int > p[N][]; //记录编号
bool vis[N];
int n, p1, p2; int Find(int x)
{
if(fa[x].rt == x) return x;
else{
int tmp = fa[x].rt;
fa[x].rt = Find(fa[x].rt);
fa[x].v = (fa[x].v + fa[tmp].v) % ;
return fa[x].rt;
}
} void Union(int x, int y, int d)
{
int fax = Find(x);
int fay = Find(y);
if(fax != fay){
fa[fay].rt = fax;
fa[fay].v = (fa[x].v + d - fa[y].v + ) % ;
}
} void solve()
{
while(~scanf("%d%d%d", &n, &p1, &p2) && (n + p1 + p2)){ int m = p1 + p2;
for(int i = ; i <= m; ++i){
fa[i].rt = i;
fa[i].v = ;
a[i][] = ;
a[i][] = ;
p[i][].clear();
p[i][].clear();
vis[i] = ; for(int j = ; j <= m; ++j) dp[i][j] = ;
} int x, y;
char op[];
for(int i = ; i <= n; ++i){
scanf("%d%d%s", &x, &y, op);
Union(x, y, op[] == 'y' ? : );
} //所有叶子完整接收信息
for(int i = ; i <= m; ++i) Find(i); int cnt = ;
for(int i = ; i <= m; ++i){
if(!vis[i]){
for(int j = ; j <= m; ++j){
if(fa[j].rt == fa[i].rt){
vis[j] = true;
a[cnt][fa[j].v]++;
p[cnt][fa[j].v].pb(j);
}
}
cnt++;
}
} dp[][] = ;
for(int i = ; i < cnt; ++i){
for(int j = ; j <= m; ++j){
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
}
}
//cout << "ans \\\\\\" << endl;
if(dp[cnt - ][p1] != ) printf("no\n");
else{
vector<int > info;
int remains = p1;
int x;
for(int i = cnt - ; i >= ; --i){
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
}
remains = x;
continue;
}
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
} remains = x;
}
} sort(info.begin(), info.end());
int sum = (int)info.size();
for(int i = ; i < sum; ++i) printf("%d\n", info[i]);
printf("end\n");
//printf("end\n\n\n");
}
}
} int main()
{ solve(); return ;
}

True Liars POJ - 1417的更多相关文章

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

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

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

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

  3. POJ1417 True Liars

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

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

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

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

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

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

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

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

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

  8. POJ 1417 True Liars

    题意:有两种人,一种人只会说真话,另一种人只会说假话.只会说真话的人有p1个,另一种人有p2个.给出m个指令,每个指令为a b yes/no,意思是,如果为yes,a说b是只说真话的人,如果为no,a ...

  9. POJ 1417 并查集 dp

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

随机推荐

  1. 1.react的基础

    1.react:专注于UI得一个js库 2.选择使用框架得原因: 写起来简单方便了,但是从稳定性上考虑得话还是原生js要稳定,所以也有很多公司直接使用原生js,但是从开发周期上来说时间会长 之前再写页 ...

  2. HashMap常问面试题整理

    去面试时,hashmap总是被经常问的问题,下面总结了几道关于hashmap的问题. 1.hashmap的主要参数都有哪些? 2.hashmap的数据结构是什么样子的?自己如何实现一个hashmap? ...

  3. Unit3-窝窝社交圈

    全文共4909字,推荐阅读时间15~20分钟. 文章共分五个部分: JML总结 作业分析 评测相关 重构策略 课程体验感受 JML总结 定义 JML是一种对Java程序进行规格化设计的表示语言 JML ...

  4. zabbix服务的部署

    1.zabbix的介绍 zabbix是一个基于WEB界面分布式系统监视以及网络监视功能的企业的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并且提供灵活的通知机制以让系统管 ...

  5. 油猴脚本 之 网教通直播评论记录抓取 v2.0

    先放一个 <油猴脚本 之 网教通直播评论记录抓取>那篇文章的传送门 . 修复内容 将所有表情转为 [符号表情] 字样,而非删除: 修复被禁言用户读取异常,现在被禁言用户表示为 张三 [已禁 ...

  6. TensorFlow从0到1之TensorFlow超参数及其调整(24)

    正如你目前所看到的,神经网络的性能非常依赖超参数.因此,了解这些参数如何影响网络变得至关重要. 常见的超参数是学习率.正则化器.正则化系数.隐藏层的维数.初始权重值,甚至选择什么样的优化器优化权重和偏 ...

  7. Anroid组件滚动视图(ScollView)简单使用

    ScollView应用展示 在xml文件中添加滚动视图 activity_main.xml <?xml version="1.0" encoding="utf-8& ...

  8. Linux环境下搭建禅道

    如何在Linux下搭建禅道 查看Linux版本信息 # cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 禅道官网下载Linux ...

  9. mysql大表在不停机的情况下增加字段该怎么处理

    MySQL中给一张千万甚至更大量级的表添加字段一直是比较头疼的问题,遇到此情况通常该如果处理?本文通过常见的三种场景进行案例说明. 1. 环境准备 数据库版本: 5.7.25-28(Percona 分 ...

  10. Java并发编程的本质是解决这三大问题

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 前言 并发编程的 ...