POJ 1417 True Liars (带权并查集,dp)

True Liars

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7080 Accepted: 2302

Description

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.

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

os:这道题的题意虽然前边都是废话,但是有很多生单词也是挺有用的,应该好好读读

  • 区分天使和魔鬼,怎样区分呢。
  • 天使说真话,魔鬼说假话
  • 任务 写一个程序来区分居民 inhabitans,对每一个数据集,如果它包含了充足的问题来区分所有的居民,打印所有天使居民的编号 (升序) 最后输出一个 end,如果在一个数据集中不包括充足的信息来辨别所有的天使编号,在一行中打印 no 不需要end

n 问题的个数 p1是天使部落的人数 p2是魔鬼部落的人数。x1 y1 a1 x1和y1是居民的编号 (1~p1+p2)

  • 分析:

x1,y1,yes x1说y1是天使,

如果x1是天使那么 y1也是天使

如果x1是魔鬼那么 y1也是魔鬼

x1,y1, no x1说y1是魔鬼

如果x1是天使那么y1是魔鬼

如果x1是魔鬼那么y1是天使

可知:为yes时,x1 y1 是同类。为no时,x1 y1 是异类

r[i] = 1 代表 i 与 根节点 为 异类。r[i] = 0 代表 i 与 根节点 为 同类

每个集合里面的编号可以分为两部分,和根节点为同类的节点,以及和根节点异类的节点

dp[i][j]表示到第i个集合选择种类的和为j的方法总数,即dp[tot][p]==1时能确定答案;对于dp过程中的每个选择我们用choice数组记录,然后选择路径用path数组记录路径

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#define Max 666
using namespace std;
int n, p1, p2;
int x1, y1;
string a1;
int r[Max];//与根节点的关系
int fa[Max],grp[Max];//所有编号属于的集合
int tag[Max][2];//用tag数组存储每个集合两种种类的数目
int dp[Max][Max];//表示第i个集合选择种类的和为j的方法总数
//dp[tot][p]==1为1是能确定答案
int tot;//存储集合的个数
int path[Max][2];//选择路径
int choice[Max][Max];//dp过程的每个选择
void init() {
    for(int i = 1; i <= p1+p2; i++) {
        fa[i] = i;
        r[i] = 0;
    }
    tot = 0;
    memset(grp, 0, sizeof(grp));
    memset(dp, 0, sizeof(dp));
    memset(path, 0, sizeof(path));
    memset(choice, 0, sizeof(choice));
    memset(tag, 0, sizeof(tag));
}
int Find(int x){
    while(fa[x] != fa[fa[x]]) {
        r[x] = r[x]^r[fa[x]];
        fa[x] = fa[fa[x]];
    }
    return fa[x];
}
void mix(int x, int y, int d) {
    int fx = Find(x);
    int fy = Find(y);
    if(fx != fy) {
        fa[fy] = fx;
        r[fy] = r[x] ^ r[y] ^ d; 
    }
}
int main() {
    ios::sync_with_stdio(false);
    while(cin >> n >> p1 >> p2){
        if(n == 0 && p1 == 0 && p2 == 0) break;
        init();
        for(int i = 0; i < n; i++){
            int d = 1;
            cin >> x1 >> y1 >> a1;
            if (a1[0] == 'y') d = 0;
            mix(x1, y1, d);
        }
        //统计集合个数并编号
        for(int i = 1; i <= p1+p2; i++) {
            if(Find(i) == i) {
                grp[i] = ++tot;
            }
        }
        //分别统计每个集合两类的数目并存储到tag中
        for(int i = 1; i <= p1+p2; i++) {
            tag[grp[Find(i)]][r[i]]++;
        }
        //dp[i][j]存储第i个集合选择种类和为j的方法数
        dp[0][0] = 1;
        for(int i = 1; i <= tot; i++) {
            for(int j = 0; j <= p1+p2; j++) {
                //同类
                if(j - tag[i][0] >= 0 && dp[i-1][j-tag[i][0]]){
                    dp[i][j] += dp[i-1][j-tag[i][0]];
                    choice[i][j] = tag[i][0];//记录路径,即选1还是选0
                }
                //异类
                if(j - tag[i][1] >= 0 && dp[i-1][j-tag[i][1]]){
                    dp[i][j] += dp[i-1][j-tag[i][1]];
                    choice[i][j] = tag[i][1];
                }
            }
        }
        if(dp[tot][p1] != 1) {
            cout << "no" << endl;
        }else {
            //标记路径
            for (int i = tot, j = p1; j > 0 && i > 0; i--) {
                if(choice[i][j] == tag[i][0]) {
                    path[i][0] = 1;
                }else path[i][1] = 1;
                j -= choice[i][j];
            }
            for(int i = 1; i <= p1+p2; i++) {
                if (path[grp[Find(i)]][r[i]]) {
                    cout << i << endl;
                }
            }
            cout << "end" << endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值