1. 程式人生 > >POJ 1417 - True Liars - [並查集+DP]

POJ 1417 - True Liars - [並查集+DP]

amp IE rip int end struct ant 所有 等於 是否

題目鏈接:http://poj.org/problem?id=1417

Time Limit: 1000MS Memory Limit: 10000K

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

題意:

有一個人流落荒島,島上有好人陣營和壞人陣營,但是他沒有辦法從外表進行分別;

然後他決定問島上的人X一個問題,問Y是不是好人;

好人陣營的人必須說真話,壞人陣營必須說謊話;

現在他只知道島上好人陣營有p1個人,壞人陣營有p2個人,想要問n個問題,期望能確定每個人是否為好人……

若不能,則輸出no;若能,則輸出輸出好人陣營的所有人的編號。

題解:

問X這個人Y是不是好人……

  若得到的回答是NO:假設X是好人,則Y是壞人;假設X是壞人,則Y是好人。說明X和Y必然是敵對陣營的。

  相反的,若得到的是YES答案,則X和Y是同一陣營的。

用並查集建樹,每個節點有兩個參數,par[x]和val[x],par[x]表示x的父親節點的編號,val[x]表示x與父親節點是不是同一陣營;

val[x]為0表示該節點與其父親節點是同陣營,val[x]為1表示該節點與其父親節點是敵對陣營。

則用並查集對n個問題進行建樹處理完之後,就將p1+p2個人分為k棵樹(k個集合),每個集合上有a個人是一個陣營,b個是另一陣營,(但不確定哪個是好人陣營,哪個是壞人陣營);

現在我們要做的就是構建一個方案:對k個集合,每個集合裏選擇a或b,認為它是這個集合內好人的人數,然後全部加起來正好等於p1;

若有且僅有這樣的一個方案,那麽就能精確判斷那些人是好人陣營的,否則就只能輸出no了。

AC代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=300+300+10;

int n,p1,p2;

int par[maxn];
bool val[maxn];
void init(int l,int r)
{
    for(int i=l;i<=r;i++) par[i]=i,val[i]=0;
}

int find(int x)
{
    if(par[x]==x) return x;
    else //下面三步非常關鍵,順序不能顛倒,要理解val[x]跟隨par[x]變動的意義
    {
        int tmp=find(par[x]);
        val[x]=val[x]^val[par[x]];
        return par[x]=tmp;
    }
}

struct Tree{
    int a,b;
    vector<int> anode;
    vector<int> bnode;
}tree[maxn];
bool treeRootAppeared[maxn];
int treeID[maxn];

struct DP{
    int val;
    int pre;
}dp[maxn][maxn];

int main()
{
    while(scanf("%d%d%d",&n,&p1,&p2) && n+p1+p2!=0)
    {
        init(1,p1+p2);
        for(int i=1,a,b;i<=n;i++)
        {
            char tmp[5];
            scanf("%d%d%s",&a,&b,tmp);

            int t1=find(a),t2=find(b);
            if(t1!=t2)
            {
                par[t1]=t2;
                if(tmp[0]==n) val[t1]=!(val[a]^val[b]); //a,b兩者是敵對陣營
                else val[t1]=val[a]^val[b]; //a,b兩者是同陣營
            }
        }

        //對每個集合內的兩個陣營人數進行統計
        for(int i=1;i<=p1+p2;i++) //初始化每個集合
        {
            tree[i].a=tree[i].b=0;
            tree[i].anode.clear();
            tree[i].bnode.clear();
        }
        int cnt=1;
        memset(treeRootAppeared,0,sizeof(treeRootAppeared));
        for(int i=1;i<=p1+p2;i++)
        {
            int t=find(i);

            if(!treeRootAppeared[t]) //這個集合的樹根還未出現過
            {
                treeRootAppeared[t]=1;
                if(val[i]==0)
                {
                    tree[cnt].a++;
                    tree[cnt].anode.push_back(i);
                }
                else
                {
                    tree[cnt].b++;
                    tree[cnt].bnode.push_back(i);
                }
                treeID[i]=treeID[t]=cnt;
                cnt++;
            }
            else //這個集合的樹根出現過了
            {
                if(val[i]==0)
                {
                    tree[treeID[t]].a++;
                    tree[treeID[t]].anode.push_back(i);
                }
                else
                {
                    tree[treeID[t]].b++;
                    tree[treeID[t]].bnode.push_back(i);
                }
                treeID[i]=treeID[t];
            }
        }

        //動態規劃求方案數
        memset(dp,0,sizeof(dp));
        dp[0][0].val=1;
        for(int i=1;i<cnt;i++)
        {
            for(int j=0;j<=p1;j++)
            {
                if(j>=tree[i].a && dp[i-1][j-tree[i].a].val>0)
                {
                    dp[i][j].val+=dp[i-1][j-tree[i].a].val;
                    dp[i][j].pre=j-tree[i].a;
                }
                if(j>=tree[i].b && dp[i-1][j-tree[i].b].val>0)
                {
                    dp[i][j].val+=dp[i-1][j-tree[i].b].val;
                    dp[i][j].pre=j-tree[i].b;
                }
            }
        }

        if(dp[cnt-1][p1].val!=1) printf("no\n");
        else
        {
            vector<int> ans;
            for(int i=cnt-1,j=p1; i>=1; j=dp[i][j].pre,i--)
            {
                int tmp=j-dp[i][j].pre; //tmp為第i個集合內好人的人數
                if(tmp==tree[i].a)
                    for(int k=0;k<tree[i].anode.size();k++) ans.push_back(tree[i].anode[k]);
                else
                    for(int k=0;k<tree[i].bnode.size();k++) ans.push_back(tree[i].bnode[k]);
            }

            sort(ans.begin(),ans.end());
            for(int i=0;i<ans.size();i++) printf("%d\n",ans[i]);
            printf("end\n");
        }
    }
}

POJ 1417 - True Liars - [並查集+DP]