poj 1417 True Liars(및 컬렉션 +dp)

9274 단어
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는 같은 종류의 사람이 아니다. 모든 관계가 있는 집합을 사용하고 유지한다. 집합 저장과 루트의 관계가 같은 사람이 몇 명, 다른 사람이 몇 명인지 조사한 다음에 dp를 한다.
pp[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-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];
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
",res[i]); } printf("end
"); } else printf("no
"); } 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 2 2 3 no 3 4 no 4 3 no 5 5 yes 5 3 no 5 5 yes 1 5 yes */

좋은 웹페이지 즐겨찾기