程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 1417 True Liars (並查集+dp)

poj 1417 True Liars (並查集+dp)

編輯:C++入門知識

True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1776 Accepted: 535

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


題意:

給出p1+p2個人,其中p1個是好人,p2個是壞人。好人只說真話,壞人只說假話,有一些關系 ,a說b是好人(壞人).其中沒有矛盾的,判斷是否有唯一解判斷哪些人是好人,哪些人是壞人。


思路來源於:cxlove博客

思路:

a說b是好人,那麼a、b是同種人的,a說b是壞人,那麼a、b不是同種人,用並查集維護每個有關系的集合,集合存和root關系相同的有多少人、不同的有多少人,然後dp。

dp[i][j] 前i個集合有j個好人的種數。

用一個數組紀錄路徑,不過要輸出方案貌似還要一點處理,我是通過建圖處理的,代碼寫的好長,衰~


代碼:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans,cnt,tot,flag;
int p1,p2;
int num[maxn][2],pre[maxn],dp[maxn][maxn],vis[maxn],pp[maxn];
int path[maxn][maxn],mp[maxn],ok[maxn][maxn],tmp[maxn],vv[maxn];
int X[maxn],Y[maxn];
char s[maxn][10];
struct Node
{
    int v,w,next;
} edge[10005];
vectorres;

void addedge(int u,int v,int w)
{
    tot++;
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=pp[u];
    pp[u]=tot;
}
int Find(int x)
{
    if(x==pre[x]) return x;
    pre[x]=Find(pre[x]);
    return pre[x];
}
void Merge(int x,int y)
{
    int u=Find(x),v=Find(y);
    if(u!=v)
    {
        if(ok[u][v])
        {
            pre[u]=v;
            num[v][1]+=num[u][1];
            num[v][0]+=num[u][0];
        }
        else
        {
            pre[u]=v;
            num[v][1]+=num[u][0];
            num[v][0]+=num[u][1];
        }
    }
}
void dfs(int u,int k,int r)  // k-是否與根相同 r-要找的點
{
    if(k==r) res.push_back(u);
    int i,j,t,v;
    for(i=pp[u]; i; i=edge[i].next)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            dfs(v,k^(!edge[i].w),r);
        }
    }
}
void dfs1(int u,int k)
{
    tmp[u]=k;
    int i,j,t,v;
    for(i=pp[u];i;i=edge[i].next)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=vv[v]=1;
            dfs1(v,k^(!edge[i].w));
        }
    }
}
void presolve()  // 預處理ok[i][j]  i和j的屬性是否相同
{
    int i,j,t;
    memset(vis,0,sizeof(vis));
    for(i=1; i<=p1+p2; i++)
    {
        if(!vis[i])
        {
            memset(tmp,0,sizeof(tmp));
            memset(vv,0,sizeof(vv));
            vis[i]=vv[i]=1;
            dfs1(i,1);
            vectortt,tx;
            for(j=1;j<=p1+p2;j++)
            {
                if(vv[j]) tt.push_back(j),tx.push_back(tmp[j]);
            }
            for(j=0;j=num[x][0])
                {
                    dp[cnt][j]+=dp[cnt-1][j-num[x][0]];
                    if(dp[cnt][j])
                    {
                        path[cnt][j]=j-num[x][0];
                    }
                }
                if(j>=num[x][1])
                {
                    int last=dp[cnt][j];
                    dp[cnt][j]+=dp[cnt-1][j-num[x][1]];
                    if(dp[cnt][j]!=last)
                    {
                        path[cnt][j]=j-num[x][1];
                    }
                }

            }
        }
    }
    res.clear();
    if(dp[cnt][p1]==1)
    {
        int now=p1;
        for(i=cnt; i>=1; i--)
        {
            int cxx=path[i][now],k;
            if(now-cxx==num[mp[i]][1]) k=1;
            else k=0;
            if(now==cxx) continue ;
            memset(vis,0,sizeof(vis));
            vis[mp[i]]=1;
            dfs(mp[i],1,k);
            now=cxx;
        }
        sort(res.begin(),res.end());
        for(i=0; i


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved