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

True Liars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16338    Accepted Submission(s): 5724

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3038

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

题意:

给出p1个好人,p2个坏人,这里面好人只说真话,坏人只说假话。

然后会有回答x y yes/no,代表的意思是x说y是好人/坏人。

最后就问你能否通过这些回答判断出哪些是好人(个数等于p1)并且输出。

题解:

这题我当时只是把思路想出来了,最后代码的实现并没有独立完成,主要是代码的后半部分...

我们先分析题目,假定x说y是好人,那么现在x有两种情况(好/坏),根据这两种情况也可以确定出y的好坏;同理,如果x说y是坏人,也有两种情况。通过对这两种情况的分析,我们会发现,当x说y是好人是,他们是同类的;当x说y是坏人时,他们不是同类的。

根据这个我们可以想到带权的并查集,用数组v[x]代表x与其父节点的关系,当v[x]为0时x与其父亲同类,为1时不同类。由于这是一个环状的关系,所以会模2。

假定我们已经分好了类,那么就会有n个集合,每个集合有与父节点同类的,也有不同类的。

如果我们现在要确定出好人的数量,那么在每个集合里面只能选一种,这时就用dp来处理:设dp[i,j]的含义是处理到第i个集合时,和为j的方案总数。

那么初始化dp[0,0]=1,转移方程为dp[i,j]+=dp[i-1,j-k0,k1],k0,k1为i集合中的两类的数量。

最后输出路径的时候有许多种方法,有兴趣的可以看下其它的代码~

最后注意,如果p1等于0,也会输出一个end。我就是在这里被坑了好久。

具体代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int N = 1505 ;
int n,p1,p2,cnt;
int f[N],dp[N][N],v[N],g[N],set[N][5];

int find(int x){
    if(f[x]==x) return x;
    int tmp = f[x];
    f[x]=find(f[x]);
    v[x]=(v[x]+v[tmp])%2;
    return f[x];
}

int main(){
    while(~scanf("%d%d%d",&n,&p1,&p2)){
        if(!n && !p1 &&!p2) break;
        cnt = 0;memset(set,0,sizeof(set));memset(dp,0,sizeof(dp));
        memset(f,-1,sizeof(f));
        for(int i=1;i<=p1+p2;i++) f[i]=i,v[i]=0;
        char s[10];int x,y;
        for(int k=1;k<=n;k++){
            scanf("%d%d %s",&x,&y,s);
            int fx=find(x),fy=find(y);
            if(fx==fy) continue ;
            f[fx]=fy;
            if(s[0]==‘y‘) v[fx]=(v[x]+v[y])%2;
            else v[fx]=(v[x]+v[y]+1)%2;
        }
        for(int i=1;i<=p1+p2;i++){
            if(find(i)==i) g[i]=++cnt;
        }
        for(int i=1;i<=p1+p2;i++) set[g[find(i)]][v[i]]++;//set数组记录第几组两类的个数
        dp[0][0]=1;//dp[i,j]前i个集合,和为j的情况数量
        for(int i=1;i<=cnt;i++){
            for(int j=0;j<=p1;j++){  //注意p1等于0的情况
                if(j>=set[i][0]) dp[i][j]+=dp[i-1][j-set[i][0]];
                if(j>=set[i][1]) dp[i][j]+=dp[i-1][j-set[i][1]];
            }
        }
        int tmp = p1;
        int choose[N];
        memset(choose,-1,sizeof(choose));
        if(dp[cnt][p1]==1){
            for(int i=cnt;i>=1;i--){
                if(dp[i-1][tmp-set[i][0]]==dp[i][tmp]){
                    choose[i]=0;
                    tmp-=set[i][0];
                }else if(dp[i-1][tmp-set[i][1]]==dp[i][tmp]){
                    choose[i]=1;
                    tmp-=set[i][1];
                }
            }
            for(int i=1;i<=p1+p2;i++){
                if(choose[g[find(i)]]==v[i]) printf("%d\n",i);
            }
            puts("end");
        }else puts("no");
    }
    return 0;
}
/*
2 0 2
1 2 yes
2 1 yes
*/ 

原文地址:https://www.cnblogs.com/heyuhhh/p/9998670.html

时间: 2024-10-12 09:32:53

POJ1417:True Liars(DP+带权并查集)的相关文章

poj1417 带权并查集 + 背包 + 记录路径

True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 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 exha

hdu 1558 Segment set【基础带权并查集+计算几何】

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3599    Accepted Submission(s): 1346 Problem Description A segment and all segments which are connected with it compose a segment set

codeforces 687D Dividing Kingdom II 带权并查集(dsu)

题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分析:把当前l到r的边进行排序,从大到小,从大的开始不断加边,判断当前能否形成二分图,如果能形成二分图,继续加边 如果不能形成二分图,那当前边的权值就是最小耗费(是不是很眼熟) 思路很清晰,现在我们要解决的是如何判断可以形成二分图,有两种,一个是2染色当前图(肯定超时) 所以只剩一种方法,带权并查集

UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍历整个图,那么编程要注意了,应该从每个点出发遍历一次. s2.带权并查集来判断,略复杂.先略过.先上个博客:http://blog.csdn.net/zsc09_leaf/article/details/6727622 c.邻接矩阵,bfs #include<iostream> #include&

HDU 5176 The Experience of Love (带权并查集 + 贪心)

The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 275    Accepted Submission(s): 111 Problem Description A girl named Gorwin and a boy named Vivin is a couple. They arrived

POJ 2492 A Bug&#39;s Life (带权并查集 &amp;&amp; 向量偏移)

题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配关系. 分析 : 本题雷同POJ 1182 食物链, 如果会了那一题, 那现在这题便简单多了, 建议先了解食物链的偏移向量做法.这里也是使用向量的思考方式来进行relation的变化, 这里我令 relation = 0为同性, relation = 1为异性, 接下来的步骤就和食物链的雷同了. 优

hdu3047Zjnu Stadium 带权并查集

//n列个座位,排数为无穷 //m个询问 //a,b,x ,a在b前面x列 //问这m个询问与其前面询问冲突的有多少个 //带权并查集存下每个点到这个集合中最前的距离 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 50010 ; int F[maxn] ; int v[maxn] ; int n , m ; int find(int

并查集——poj2236(带权并查集)

题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N) :表示修理计算机p: "S p q" (1 <= p, q <= N) :表示检测计算机p和计算机q能否通信. 输出:能通信则输出"SUCCESS",否则输出"FAIL" 题解: 带权并查集还是那个重要的知识点--关系. 此题,我们使

【bzoj 1202】[HNOI2005] 狡猾的商人(图论--带权并查集+前缀和)

题意:一个账本记录了N个月以来的收入情况,现在有一个侦探员不同时间偷看到M段时间内的总收入,问这个账本是否为假账. 解法:带权并查集+前缀和.   判断账本真假是通过之前可算到的答案与当前读入的值是否相同来完成.那么就是只有知道新读入的区间2端的(在相同区域内的!!)前缀和才可以判断,也就是这2个端点之前被纳入了相同的区域内才可以判断.于是,我们就可以想到并查集了.(( ′? ??`) 真的么......)   假设已知x~y月的总收入为d,那么s[y]-s[x-1]=d.一般前缀和是算上自己的