pat-1149 Dangerous Goods Packaging(25)(用个flag数组就行的超简单法)

 

When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent (氧化剂) must not be packed with flammable liquid (易燃液体), or it can cause explosion.

Now you are given a long list of incompatible goods, and several lists of goods to be shipped. You are supposed to tell if all the goods in a list can be packed into the same container.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: N (≤10​4​​), the number of pairs of incompatible goods, and M (≤100), the number of lists of goods to be shipped.

Then two blocks follow. The first block contains N pairs of incompatible goods, each pair occupies a line; and the second one contains M lists of goods to be shipped, each list occupies a line in the following format:

K G[1] G[2] ... G[K]

where K (≤1,000) is the number of goods and G[i]'s are the IDs of the goods. To make it simple, each good is represented by a 5-digit ID number. All the numbers in a line are separated by spaces.

Output Specification:

For each shipping list, print in a line Yes if there are no incompatible goods in the list, or No if not.

Sample Input:

6 3
20001 20002
20003 20004
20005 20006
20003 20001
20005 20004
20004 20006
4 00001 20004 00002 20003
5 98823 20002 20003 20006 10010
3 12345 67890 23333

Sample Output:

No
Yes
Yes

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

昨天考试的思路,今天同学问我我就又敲了一遍~

思路:

1.结构体存放两两一对不能一起的买的物品a,b

2.把购物清单中的物品对应的flag标记为1

3.查询每一对不能一起买的物品的flag值是否同时为1

#include 
#include 
#include 
using namespace std;
const int maxn = 1e5 + 5;
typedef struct{
    int a,b;
} node;
node s[maxn];
int flag[maxn];
int main() 
{
    int n,m,k,x;
    scanf("%d%d",&n,&m);
    for(int i =0;i < n;i++)
        scanf("%d%d",&s[i].a,&s[i].b);
    for(int i = 0;i < m;i++)
    {
        memset(flag,0,sizeof(flag));
        int f1 = 0;
        scanf("%d",&k);
        for(int j = 0;j < k;j++)
        {
            scanf("%d",&x);
            flag[x] = 1;
        }
        for(int j = 0;j < n;j++)
        {
            if(flag[s[j].a] == 1 && flag[s[j].b] == 1)
            {
                printf("No\n");
                 f1 = 1;
                break;
            }
        }
        if(!f1)
            printf("Yes\n");
    }
    return 0;
}

 

你可能感兴趣的:(pat甲级)