ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ1417 True Liars

2022-07-10 23:34:22  阅读:127  来源: 互联网

标签:p2 cnt p1 int POJ1417 fa Liars True dp


题目链接

题目

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

Japan 2002 Kanazawa

题解

方法一

知识点:并查集,背包dp。

这题第一眼看上去用扩展域并查集能做,但确实能做,就是烦死人。因为扩展域并查集不记录元素之间的相对关系,之后不是判断条件矛盾,而是需要把每个关系集合的各类人数具体记录,因此一开始就记录在一个关系集合的带权并查集会更好,免去了查找某个元素在哪个关系集合的麻烦。

时间复杂度 \(O(n\log(p_1+p_2) + np_1)\)

空间复杂度 \(O(p_1\cdot (p_1+p_2))\)

方法二

知识点:并查集,背包dp。

用带权并查集是最优解,在处理完关系后能直接分类。随后是一个简单的计数dp,如果 \(dp[tot][p1] = 1\) 则说明有且仅有一种方法能凑成 \(p1\) 的好人,所以是可行的,然后逆推求每次选择在各个关系集合选择类别,最后遍历编号,输出对应集合选择类别和自己相应的人的编号即可。

时间复杂度 \(O(n\log(p_1+p_2) + np_1)\)

空间复杂度 \(O(p_1\cdot (p_1+p_2))\)

代码

方法一

#include <bits/stdc++.h>

using namespace std;

int fa[1207], feat[607], num[1207], cnt[607][2], dp[607][607], ans[607];

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y) {
    fa[find(x)] = find(y);
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, p1, p2;
    while (cin >> n >> p1 >> p2, n || p1 || p2) {
        int m = p1 + p2;
        memset(feat, 0, sizeof(feat));
        memset(num, 0, sizeof(num));
        memset(cnt, 0, sizeof(cnt));
        memset(dp, 0, sizeof(dp));
        for (int i = 1;i <= 2 * m;i++) fa[i] = i;
        for (int i = 1;i <= n;i++) {
            int x, y;
            string a;
            cin >> x >> y >> a;
            if (a == "yes") {
                merge(x, y);
                merge(x + m, y + m);
            }
            else if (a == "no") {
                merge(x, y + m);
                merge(x + m, y);
            }
        }

        ///扩展域(种类)并查集把每个元素的所有可能扩展为一个独立的种类元素,将等价的种类元素(必然同时出现)放入一个集合,构成
        ///因为种类元素列出了所有可能,故可以容易检验新关系的合法性,只需检验与新关系矛盾的所有集合是否存在
        ///因为构造了等价类,所以容易检验元素的等价关系(能否同时出现),但访问在同一关系集合中原元素的互相的具体种类关系需要划分等价类
        ///确立一个标准等价类,其他同一关系集合的等价类元素以此基准分类,难点在需要检验标准等价类是否已经存在

        ///带权并查集将同一关系集合的元素放入一个集合,用权值确定一个集合的元素互相的具体种类关系,使得划分了等价类非常容易
        ///通过权值可以容易检验新关系的合法性,可以容易访问元素的种类关系(等价关系更容易)

        int tot = 0;
        for (int i = 1;i <= m;i++) {
            if (!num[find(i)] && !num[find(i + m)]) num[find(i)] = ++tot;
            if (!num[find(i)]) feat[i] = 1, cnt[num[find(i + m)]][1]++;
            else if (!num[find(i + m)]) feat[i] = 0, cnt[num[find(i)]][0]++;
        }
        ///如果自己等价类和其他同一关系集合的等价类都没编号,则给自己等价类记录编号,自己等价类和其他等价类有且仅有一个有编号,作为基准
        ///权值是相对于基准等价类
        ///如果自己集合没编号,则自己在标准等价类的同一关系集合下的其他等价类,根据种类位置,种类值为1
        ///如果自己集合有编号,则自己在标准等价类,因此种类值为0
        dp[0][0] = 1;
        for (int i = 1;i <= tot;i++) {
            for (int j = 0;j <= p1;j++) {
                if (j >= cnt[i][0]) dp[i][j] = dp[i - 1][j - cnt[i][0]];
                if (j >= cnt[i][1]) dp[i][j] += dp[i - 1][j - cnt[i][1]];
            }///简单的计数DP
        }
        if (dp[tot][p1] == 1) {
            for (int i = tot, j = p1;i >= 1;i--) {
                if (dp[i][j] == dp[i - 1][j - cnt[i][0]]) ans[i] = 0, j -= cnt[i][0];
                else ans[i] = 1, j -= cnt[i][1];
            }
            for (int i = 1;i <= m;i++) {
                if (feat[i] == ans[num[find(i)] ? num[find(i)] : num[find(i + m)]]) cout << i << '\n';
            }
            ///扩展域并查集遗留问题,没有把同一关系集合元素放在一起
            cout << "end" << '\n';
        }
        else cout << "no" << '\n';
    }
    return 0;
}

方法二

#include <bits/stdc++.h>

using namespace std;

int fa[607], num[607], cnt[607][2], dp[607][607], ans[607];
bool v[607];///0表示与父节点同类,1表示异类

int find(int x) {
    if (fa[x] == x) return x;
    int pre = fa[x];
    fa[x] = find(fa[x]);
    v[x] ^= v[pre];
    return fa[x];
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, p1, p2;
    while (cin >> n >> p1 >> p2, n || p1 || p2) {
        int m = p1 + p2;
        memset(v, 0, sizeof(v));
        memset(num, 0, sizeof(num));
        memset(cnt, 0, sizeof(cnt));
        memset(dp, 0, sizeof(dp));
        for (int i = 1;i <= m;i++) fa[i] = i;
        for (int i = 1;i <= n;i++) {
            int x, y;
            string a;
            cin >> x >> y >> a;
            bool z = a == "no";
            int rx = find(x);
            int ry = find(y);
            v[rx] = v[x] ^ z ^ v[y];
            fa[rx] = ry;
        }
        int tot = 0;
        for (int i = 1;i <= m;i++) {
            int ri = find(i);
            if (!num[ri]) num[ri] = ++tot;
            cnt[num[ri]][v[i]]++;
        }
        dp[0][0] = 1;
        for (int i = 1;i <= tot;i++) {
            for (int j = 0;j <= p1;j++) {
                if (j >= cnt[i][0]) dp[i][j] = dp[i - 1][j - cnt[i][0]];
                if (j >= cnt[i][1]) dp[i][j] += dp[i - 1][j - cnt[i][1]];
            }
        }
        if (dp[tot][p1] == 1) {
            for (int i = tot, j = p1;i >= 1;i--) {
                if (dp[i][j] == dp[i - 1][j - cnt[i][0]]) ans[i] = 0, j -= cnt[i][0];
                else ans[i] = 1, j -= cnt[i][1];
            }
            for (int i = 1;i <= m;i++) {
                if (v[i] == ans[num[find(i)]]) cout << i << '\n';
            }
            cout << "end" << '\n';
        }
        else cout << "no" << '\n';
    }
    return 0;
}

标签:p2,cnt,p1,int,POJ1417,fa,Liars,True,dp
来源: https://www.cnblogs.com/BlankYang/p/16464419.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有