Blood groups

Problem description
There are four possible blood groups for humans: AB, A, B and O, meaning that the red blood cells have antigens of types, respectively, A and B, only A, only B, and no antigen at all. Our blood group is determined by two alleles in our DNA. Each allele is of type either A, B or O. The following table lists the possible allele combinations someone may have for each blood group:


We inherit exactly one allele from each of our two parents. So, given the blood groups of the two parents, we can say for sure if some blood group is possible, or not, in their offspring. For example, if the blood groups of the two parents are AB and B, then the possible allele combinations for them are, respectively, {AB} and {OB,BB}. Since the order of the alleles does not matter, the possible allele combinations for the offspring are {OA,AB,OB,BB}. That means the blood groups AB, A and B are possible in their offspring, but the blood group O is not. Very nice indeed! But what if life on Earth had evolved so that a person had three parents, three alleles, and three different antigen types? The allele combinations would look like this:

If the blood groups of the three parents are A, BC and O, then all blood groups are possible in their offspring, except groups BC and ABC.
The universe is vast! There may be, out there in space, some form of life whose individuals have N parents, N alleles, and N different antigen types. Given the blood groups for the N parents, and a list of Q blood groups to test, your program has to determine which ones are possible, and which ones are not, in the offspring of the given parents.

Input
The first line contains two integers N and Q, representing respectively the number of parents (and alleles, and antigen types) and the number of queries (1 ≤ N ≤ 100 and 1 ≤ Q ≤ 40). Each of the next N lines describes the blood group of a parent. After that, each of the next Q lines describes a blood group to test. Antigen types are identified with distinct integers from 1 to N, not letters. Each line describing a blood group contains an integer B indicating the number of antigen types in the blood group (0 ≤ B ≤ N), followed by B different integers C1,C2,...,CB representing the antigen types present in the blood group (1 ≤ Ci ≤ N for i = 1,2,...,B).

Output
For each of the Q queries, output a line with the uppercase letter "Y" if the corresponding blood group is possible in the offspring of the given parents; otherwise output the uppercase letter "N". Write the results in the same order that the queries appear in the input.


Sample Input
Sample input 1
2 1
2 2 1
1 2
0

Sample input 2
3 4
1 1
2 2 3
0
1 3
3 2 1 3
2 1 2
2 3 2

Sample input 3
4 3
4 2 1 3 4
4 2 1 3 4
1 1
1 2
1 3
2 2 1
0
Sample Output
Sample output 1
N

Sample output 2
Y
N
Y
N

Sample output 3
Y
Y
N
Problem Source
ICPC Latin American Regional -2015

题解:二分匹配

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=105;
int n,q,p[maxn],ch[maxn];
bool g[maxn][maxn],vis[maxn];
bool solve(int u){
    for(int i=0;i<n;i++)
        if(!vis[i]&&g[i][u]){
            vis[i]=1;
            if(p[i]==-1||solve(p[i]))
                {p[i]=u;return true;}
        }
    return false;
}
int main(){
    scanf("%d%d",&n,&q);
    for(int num,i=0;i<n;i++){
        scanf("%d",&num);
        for(int tmp,j=0;j<num;j++){
            scanf("%d",&tmp);
            g[i][tmp]=1;
        }
        if(num<n) g[i][0]=1;
    }
    while(q--){
        int num,ans=0;
        scanf("%d",&num);
        memset(p,-1,sizeof(p));
        for(int i=0;i<num;i++){
            scanf("%d",&ch[i]);
            memset(vis,0,sizeof(vis));
            if(solve(ch[i])) ans++;
        }
        if(ans<num) {printf("%c\n",'N');continue;}
        //printf("%d\n",ans);
        if(num<n){
            for(int i=0;i<n;i++)
            if(p[i]==-1){
            	if(g[i][0]) {ans++;continue;}
                for(int j=0;j<num;j++)
                if(g[i][ch[j]]) {ans++;break;}
            }
        }
        printf("%c\n",ans==n?'Y':'N');
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值