poj 1417 True Liars (并查集+dp)

来源:互联网 发布:淘宝手提包品牌 编辑:程序博客网 时间:2024/06/06 14:15
True Liars
Time Limit: 1000MS Memory Limit: 10000KTotal 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 11 2 no2 1 no3 2 11 1 yes2 2 yes3 3 yes2 2 11 2 yes2 3 no5 4 31 2 yes1 3 no4 5 yes5 6 yes6 7 no0 0 0

Sample Output

nono12end3456end

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 <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#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-6typedef 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];vector<int>res;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);            vector<int>tt,tx;            for(j=1;j<=p1+p2;j++)            {                if(vv[j]) tt.push_back(j),tx.push_back(tmp[j]);            }            for(j=0;j<tt.size();j++)            {                for(int k=j;k<tt.size();k++)                {                    int u=tt[j],v=tt[k];                    if(tx[j]==tx[k]) ok[u][v]=ok[v][u]=1;                    else ok[u][v]=ok[v][u]=0;                }            }        }    }}void solve() // dp并输出答案{    int i,j,t;    memset(dp,0,sizeof(dp));    memset(vis,0,sizeof(vis));    dp[0][0]=1;    cnt=0;    for(i=1; i<=p1+p2; i++)    {        int x=Find(i);        if(!vis[x])        {            cnt++;            mp[cnt]=x;            vis[x]=1;            for(j=0; j<=p1; j++)            {                if(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<res.size(); i++)        {            printf("%d\n",res[i]);        }        printf("end\n");    }    else printf("no\n");}int main(){    int i,j,t;    while(scanf("%d%d%d",&n,&p1,&p2),n|p1|p2)    {        int u,v;        tot=0;        memset(pp,0,sizeof(pp));        for(i=1; i<=p1+p2; i++)        {            pre[i]=i;            num[i][1]=1;            num[i][0]=0;        }        for(i=1; i<=n; i++)        {            scanf("%d%d%s",&X[i],&Y[i],s[i]);            addedge(X[i],Y[i],s[i][0]=='y');            addedge(Y[i],X[i],s[i][0]=='y');        }        presolve();        for(i=1;i<=n;i++)        {            Merge(X[i],Y[i]);        }        solve();    }    return 0;}/*7 4 22 3 no3 4 no4 3 no5 5 yes5 3 no5 5 yes1 5 yes*/



0 0
原创粉丝点击