Feast bzoj1823 luogu4171

1823: [JSOI2010] Feast

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 3392  Solved: 1704
[Submit][Status][Discuss]

Description

Feast is the most sumptuous Chinese banquet dishes, there are many different kinds of materials through cooking methods or Manchu Han, presented in the myriad of dishes. Due to the large and complex dishes, only a handful of knowledgeable talented chef can make a Feast, and be able to cook, it is one of several of our experts honor of the Feast of the largest Chinese chef. World Association is able to Feast Feast cuisine chefs composed of experts, but among them are also broken down into a number of different levels of cooks. In order to recruit new entrants into the world of chefs Feast Association will soon organize Feast contest, Association sent many members as assessors, is to be among the participating chefs, found a rising star Manchu food industry. Rules of the General Assembly is as follows: Each participating players can get n kinds of materials, players are free to choose with full Chinese cuisine type or material as dishes. Review system of the General Assembly are: a total of m bits assessors were checks. Every reviewer for the Feast has its own unique insights, but the basic view is that there must be different from the dishes as the Feast of the flag. If a review that if there is no Chinese-style Pork with full-style lamb pot, can not be regarded Feast. But avoid too strong-minded review, the provisions of the General Assembly unless it is in a reviewer thinks should be two dishes are not done out of the situation, in order to eliminate a player, or can not be eliminated contestant. In other words, as long as the participants can practice in these two materials, one in line with the preferences of the assessment can be reviewed by the review. When materials such as pork, lamb and beef, there are like four assessors in the following table: a review of two assessment review three full review four full type-style beef pork beef Han Han Chinese-style pork, beef and mutton full style Chinese-style pork filled a style lamb as participants make full style pork, lamb and full style full of beef dishes, he will be unable to meet the requirements of accreditation III, can not pass the assessment. The contestants B to make Chinese-style pork, lamb and full style full of beef dishes, to meet the requirements of all reviews. But later we discovered that the General Assembly, under such a system if the material sent by the assessor selected with no special arrangement is good, all the participants can only examine part by reviewers but not all, so that no one may occur through the examination situation. If when four assessors preferences in the following table, regardless of what kind of competitor practices, can not pass the examination of all the assessment: a review of two assessment review three full review four full type-style lamb and mutton Chinese style Chinese-style pork Chinese-style pork, lamb and mutton full type-style Chinese-style pork full of pork

Input

The first line contains a number K, K represents the test file contains a set of data. The first line of each set of test data contains two numbers with n m (n≤100, m≤1000), representatives of n materials, m bits panelists. For convenience, the material discarded Chinese name given number, numbered from 1 to n. The next m lines, each line represents the corresponding reviewers have two preferences, each preference of a letter followed by a number represents, such as m1 on behalf of this review materials like the first one to do it through the full cuisine dishes, while h2 behalf of the reviewers like the first two through the material made out of Chinese cuisine dishes. Each test file will not have more than 50 sets of test data

Output

Of each test data output line, it will not happen if no one can assess the dilemma, the output GOOD; otherwise the output BAD (in capital letters).

Sample Input

2
3 4
m3 's H1
m1 is M2
H1 H3
H3 M2
2 4
H1 M2
M2 m1 is
H1 H2
m1 is H2

Sample Output

GOOD
BAD

Learn about the 2-SAT template theme

2-SAT problem: a collection, each element has two values, such as to give some form A take A 0/1 , then B must be fetched B 0/1 constraints, asking whether there is a solution

See the problem, the first reaction is to extend the domain disjoint-set

If, however think about the proviso: if A take A 0 , B taking B 0 , then the combined A 0 and B 0 , if this time adding a virtually B takes B 0 , then A takes A 0 constraints

So how to solve it?

The non-extension field disjoint-set into the side directed edges, strongly connected components can then seek!

 

In this problem, if the judges preferences are mx HY , so if a person did HX , then he must do HY , if done My , you have to do mx

Then is 2-SAT is a template

 

 

#include<bits/stdc++.h>
using namespace std;
const int N=105,M=1005;
int n,m;
int head[N<<1],ver[M<<1],nxt[M<<1],ce;
int co,dfn[N<<1],low[N<<1],st[N<<1],top,ins[N<<1],c[N<<1],cnt;
char ch[10];
void tarjan(int now)
{
    dfn[now]=low[now]=++co,ins[now]=1,st[++top]=now;
    for(int i=head[now];i;i=nxt[i])
    {
        int y=ver[i];
        if(!dfn[y])
        {
            tarjan(y);
            low[now]=min(low[now],low[y]);
        }
        else if(ins[y]) low[now]=min(low[now],dfn[y]);
    }
    if(dfn[now]==low[now])
    {
        ++cnt;int y=-1;
        while(y!=now)
            y=st[top--],c[y]=cnt,ins[y]=0;
    }
}
void init(int &x)
{
    scanf("%s",ch+1);
    int f=0,res=0;
    if(ch[1]=='h') f=n;
    for(int i=2;i<=strlen(ch+1);++i) res=res*10+ch[i]-'0';
    x=res+f;
}
int f(int x) {return x>n?x-n:x+n;}
void adde(int a,int b)
{
    ver[++ce]=b,nxt[ce]=head[a],head[a]=ce;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        bool flag=0;
        scanf("%d%d",&n,&m);
        ce=co=cnt=0;
        memset(head,0,sizeof(head));
        for(int i=1;i<=m;++i)
        {
            int num1,num2;
            init(num1); init(num2);
            adde(f(num1),num2); adde(f(num2),num1);
        }
        /*for(int i=1;i<=n<<1;++i)
        {
            cout<<i<<":";
            for(int j=head[i];j;j=nxt[j])
                cout<<ver[j]<<" ";
            cout<<endl;
        }*/
        memset(dfn,0,sizeof(dfn));
        for(int i=1;i<=n<< 1;++i)
        if(!dfn[i]) tarjan(i);
        //for(int i=1;i<=n<<1;++i)
        //    cout<<i<<" "<<c[i]<<endl;
        for(int i=1;i<=n;++i)
            if(c[i]==c[i+n])
            {
                puts("BAD");
                flag=1; break;
            }
        if(!flag) puts("GOOD");
    }
    return 0;
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/w19567/p/11239566.html