PAT_A1149#Dangerous Goods Packaging

Source:

PAT A1149 Dangerous Goods Packaging (25 分)

Description:

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

Keys:

  • 散列(Hash)

Attention:

  • 若1个元素与N个元素互斥,属于散列;若一系列元素相互排斥的话,则是并查集

Code:

 1 /*
 2 Data: 2019-08-04 16:38:35
 3 Problem: PAT_A1149#Dangerous Goods Packaging
 4 AC: 32:04
 5 
 6 题目大意:
 7 题目大意:
 8 给一份两两互斥的清单,再给出货品清单,判断该批货品是否兼容
 9 
10 基本思路:
11 二维数组存储各个物品的互斥集,
12 设置该批物品存在位=1,遍历该批物品的互斥集;
13 若互斥集中物品的存在位=1,则不兼容
14 */
15 #include<cstdio>
16 #include<vector>
17 #include<algorithm>
18 using namespace std;
19 const int M=1e6;
20 vector<int> st[M];
21 int exist[M],goods[M];
22 
23 int main()
24 {
25 #ifdef ONLINE_JUDGE
26 #else
27     freopen("Test.txt", "r", stdin);
28 #endif // ONLINE_JUDGE
29 
30     int n,m,k,v1,v2;
31     scanf("%d%d", &n,&m);
32     for(int i=0; i<n; i++)
33     {
34         scanf("%d%d", &v1,&v2);
35         st[v1].push_back(v2);
36         st[v2].push_back(v1);
37     }
38     while(m--)
39     {
40         scanf("%d", &k);
41         fill(exist,exist+M,0);
42         for(int i=0; i<k; i++)
43         {
44             scanf("%d", &goods[i]);
45             exist[goods[i]]=1;
46         }
47         for(int i=0; i<k; i++)
48             for(int j=0; j<st[goods[i]].size(); j++)
49                 if(exist[st[goods[i]][j]]==1){
50                     k=0;break;
51                 }
52         if(k)   printf("Yes\n");
53         else    printf("No\n");
54     }
55 
56     return 0;
57 }

 

posted @ 2019-05-16 20:40  林東雨  阅读(156)  评论(0编辑  收藏  举报