1. 程式人生 > >openJudge C17K:Lying Island

openJudge C17K:Lying Island

menu eof sla 包括 ets words 限制 ats !=

地址:http://poj.openjudge.cn/practice/C17K/

題目:

C17K:Lying Island

  • 查看
  • 提交
  • 統計
  • 提問
總時間限制:
2000ms
內存限制:
262144kB
描述

There are N people living on Lying Island. Some of them are good guys, while others are bad.

It is known that good guys are always telling truths, while bad guys may be telling either truths or lies.

One day, an investigator came to Lying Island. He wondered how many good guys are there on the island. So he conducted an investigation.

People on the island are numbered from 1 to N. When the investigator was interviewing person i, he showed the islander the information about at most K people numbered from max{i-K,1} to (i-1).

Sometimes, the i-th islander would say, "Oh, person x is a good(bad) guy."

But sometimes, the i-th islander would mince his words, "Eh, if person x is a good(bad) guy, person y is a good(bad) guy."

Of course, x and y is less than i but no less than (i-K), because person i only saw the information of that at most K islanders.

The investigator does not think he can infer exactly how many good guys on the island from these words, but he feels that he can figure out how many good guys at most on the island. Can you help him?

輸入
The first line contains an integer T (1 <= T <= 50), indicating the number of test cases.

For each test case:

The first line contains two integers N and K (0 <= N <= 5000,1 <= K <= 10).

Then (N-1) lines follow, each line contains a string, indicating the sentences said by person 2 to person N. Each sentence is in one of the following formats:
1. Person i: Person x is a good guy.
2. Person i: Person x is a bad guy.
3. Person i: If person x is a good guy, person y is a good guy.
4. Person i: If person x is a bad guy, person y is a bad guy.
It is guaranteed that in each sentence, max{1,i-K} <= x,y < i and x ≠ y.
輸出
For each test case, output one integer on a single line, indicating the number of good guys at most on the island.
樣例輸入
2
7 2
Person 2: Person 1 is a good guy.
Person 3: Person 2 is a bad guy.
Person 4: If person 3 is a good guy, person 2 is a good guy.
Person 5: If person 4 is a good guy, person 3 is a bad guy.
Person 6: If person 5 is a bad guy, person 4 is a good guy.
Person 7: If person 6 is a bad guy, person 5 is a bad guy.
7 4
Person 2: Person 1 is a bad guy.
Person 3: Person 2 is a bad guy.
Person 4: If person 3 is a good guy, person 1 is a bad guy.
Person 5: Person 2 is a bad guy.
Person 6: If person 4 is a good guy, person 2 is a bad guy.
Person 7: Person 6 is a bad guy.
樣例輸出
6
4
提示
For the first test case, person 3 is a bad guy, and others are good.
Person 1: Person 1 said nothing, he is a good guy.
Person 2: Person 1 is indeed a good guy, and person 2 is saying the truth.
Person 3: Person 2 is not a bad guy, and person 3 is lying.
Person 4: Person 3 is not a good guy, so the prerequisite does not met. Person 4 is saying the truth.
Person 5: Person 4 is indeed a good guy and person 3 is indeed a bad guy. Person 5 is saying the truth.
Person 6: Person 5 is a good guy. Person 6 is saying the truth.
Person 7: Person 6 is a good guy. Person 7 is saying the truth.
思路:狀壓DP,把包括當前位的前十位狀壓進去就好了,然後轉移。
#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-8;
const double pi=acos(-1.0);
const int K=5e3+7;
const int mod=1e9+7;

int n,ans,k,dp[K][1<<10];
char sa[200];

int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        memset(dp,-1,sizeof dp);
        for(int i=0,mx=(1<<9);i<mx;i++)
            dp[1][i]=0;
        for(int i=(1<<9),mx=(1<<10);i<mx;i++)
            dp[1][i]=1;
        for(int i=2,x,y,dx,dy;i<=n;i++)
        {
            scanf("%s%d%s%s",sa,&x,sa,sa);
            if(sa[0]==P)
            {
                scanf("%d%s%s%s",&x,sa,sa,sa);
                if(sa[0]==b)  dx=0;
                else    dx=1;
                gets(sa);
                for(int j=0,mx=1<<10;j<mx;j++)
                if(((j>>(x-i+10))&1)==dx&&~dp[i-1][j])
                    dp[i][(j+mx)>>1]=max(dp[i-1][j]+1,dp[i][(j+mx)>>1]);
                for(int j=0,mx=(1<<10);j<mx;j++)
                    dp[i][j>>1]=max(dp[i][j>>1],dp[i-1][j]);
            }
            else
            {
                scanf("%s%d%s%s%s",sa,&x,sa,sa,sa);
                if(sa[0]==b)   dx=0;
                else    dx=1;
                scanf("%s%s%d%s%s%s",sa,sa,&y,sa,sa,sa);
                if(sa[0]==b)   dy=0;
                else    dy=1;
                gets(sa);
                for(int j=0,mx=1<<10;j<mx;j++)
                if(~dp[i-1][j] && !(((j>>(x-i+10))&1)==dx&&((j>>(y-i+10))&1)!=dy))
                    dp[i][(j+mx)>>1]=max(dp[i-1][j]+1,dp[i][(j+mx)>>1]);
                for(int j=0,mx=(1<<10);j<mx;j++)
                    dp[i][j>>1]=max(dp[i-1][j],dp[i][j>>1]);
            }
        }
        ans=0;
        for(int i=0,mx=(1<<10);i<mx;i++)
            ans=max(ans,dp[n][i]);
        printf("%d\n",ans);
    }
    return 0;
}

/*
Person 2: Person 1 is a bad guy.
Person 3: Person 2 is a bad guy.
Person 4: Person 3 is a bad guy.
Person 5: Person 4 is a bad guy.

Person 2: Person 1 is a good guy.
Person 3: Person 2 is a good guy.
Person 4: Person 3 is a bad guy.
Person 5: Person 4 is a good guy.
*/

openJudge C17K:Lying Island