1. 程式人生 > >POJ1417:True Liars(DP+帶權並查集)

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 
*/