1149 Dangerous Goods Packaging (25 分)

1149 Dangerous Goods Packaging (25 分)
 

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 (≤), the number of pairs of incompatible goods, and M (≤), 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 (≤) 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

标记一下,再依次遍历就行。


题目大意:集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里。
比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸。给定一张不相容物品的清单,
需要你检查每一张集装箱货品清单,判断它们是否能装在同一只箱子里。对每箱货物清单,
判断是否可以安全运输。如果没有不相容物品,则在一行中输出 Yes,否则输出 No~

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 int n,m,k;
 5 vector<int> v[100000];
 6 int ans[100000];
 7 int main(){
 8     cin >> n >> m;
 9     int x,y;
10     for(int i = 0 ; i < n ; ++i){
11         scanf("%d%d", &x,&y);
12         v[x].push_back(y);
13         v[y].push_back(x);
14     }
15     while(m--){
16         vector<int> vt;
17         cin >> k;
18         memset(ans, 0, sizeof(ans));
19         for(int i = 0; i < k; ++i){
20             cin >> x;
21             vt.push_back(x);
22             ans[x]++;
23         }
24         bool flag = true;
25         for(int i = 0 ; i < k; i++){
26             y = vt[i];
27             for(int j = 0; j < v[y].size(); j++){
28                 if(ans[v[y][j]]){
29                     flag = false;
30                     break;
31                 }
32             }
33             if(!flag)
34                 break;
35         }
36         if(flag)
37             cout <<"Yes"<<endl;
38         else
39             cout <<"No"<<endl;
40     }
41 
42     return 0;
43 }




原文地址:https://www.cnblogs.com/zllwxm123/p/11272956.html