1. 程式人生 > >True Liars (並查集壓縮路徑 + DP)

True Liars (並查集壓縮路徑 + DP)

                                     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. 

 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 ren,一個魔鬼部落,有p2人,所有人都有編號,從1 到 p1+p2,魔鬼部落說的都是謊話 ,天使部落說的都是真話,當為yes時,如果a是天使,他的話為真,b一定是天使,若a為魔鬼時,那麼他的話為假,所以b一定也是魔鬼,當為no時,如果a為天使,他的話為真,所以,b一定是魔鬼,若a為魔鬼,a的話為假,所以b為天使,所以當為yes時,a 與 b 是同一個部落的 , 為 no 時 , a 與 b 一定不是同一個部落 。

分析:事實上這個題目前面的並查集部分只是一個普通的種類並查集,這個記錄路徑的dp才是本題解的精妙部分;我們用vis[i]來表示與父結點的關係,0表示兩者同類,1表示兩者不同類。前面是不同的帶權並查集,,你可以得到很多集合,每個集合都有兩個子集合,通過查詢dp來查詢,每個大集合的一個小集合的個數是p1。w0[] w1[]存每個大集合裡面連個小集合的個數。

通過dp[ i ][ j ] = sum(dp[ i - 1][ j - w1[ i ]] + dp[i - 1][ j  -  w0[ j ]] ) ;

如果dp[ cnt ][ p1 ] 不等於 1, 那麼輸出 no

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

const int Maxn = 1000;

int f[Maxn] , vis[Maxn];
string s;
int p[Maxn] , w1[Maxn] , w0[Maxn];
bool used[Maxn];

int dp[Maxn][Maxn];

int Find(int x){
    if(x != f[x]){
        int k = f[x];
        f[x] = Find(f[x]);
        vis[x] = (vis[x] + vis[k]) % 2;
    }
    return f[x];
}

int main(){
    int n , p1, p2 , a , b;
    while(scanf("%d%d%d",&n , & p1 , & p2) && n + p1 + p2 != 0){
        memset(dp,0,sizeof(dp));

        int  t = 0;
        for(int i = 0;i <= p1+p2;i++) f[i] = i , vis[i] = 0 , w1[i] = 0 ,w0[i] = 0, used[i] = false;       // 初始化

        while(n--){
            cin >> a >> b >> s;
            int ta = Find(a);
            int tb = Find(b);
            if(ta != tb){
                f[ta] = tb;
                if(s[0] == 'y') vis[ta] =  (vis[a] + vis[b]) % 2;
                else vis[ta] = (vis[a] + vis[b] + 1) % 2;
            }
        }
        // 求出每個集合包括兩個小集合的個數
        int cnt = 1;
        for(int i = 1;i <= p1 + p2;i++){
            if(!used[i]){
                int t = Find(i);
                for(int j = i;j <= p1 + p2;j++){
                    if(Find(j) == t && !used[j]){
                        used[j] = true;
                        if(!vis[j]) w0[cnt]++;
                        else w1[cnt]++;
                    }
                }
                p[cnt] = t; // 將集合的根結點存起來
                cnt++;
            }
        }

        dp[0][0] = 1;
        for(int i = 1;i < cnt;i++){
            int Min = min(w0[i] , w1[i]);
            for(int j = p1;j >= Min;j--){
                if(dp[i - 1][j - w0[i]]) dp[i][j] += dp[i - 1][j - w0[i]];
                if(dp[i - 1][j - w1[i]]) dp[i][j] += dp[i - 1][j - w1[i]];
            }
        }

        if(dp[cnt - 1][p1] != 1){
            printf("no\n");
            continue;
        }
        int ans[1000];
        int num = 0;
        int flag= p1;
        for(int i = cnt - 1;i >= 1;i--){
            if(dp[i-1][flag - w0[i]] == 1){
                for(int j = 1;j <= p1 + p2;j++)
                  if(Find(j) == p[i] && vis[j] == 0) ans[num++] = j;
                flag -= w0[i];
            }
            else if(dp[i-1][flag - w1[i]] == 1){
                for(int j = 1;j <= p1 + p2;j++)
                  if(Find(j) == p[i] && vis[j] == 1) ans[num++] = j;
                flag -= w1[i];
            }
        }
        sort(ans ,ans + num);
        for(int i = 0;i < num;i++) printf("%d\n",ans[i]);
        cout << "end\n";
    }
    return 0;
}