A1149 Dangerous Goods Packaging (25 point(s))

危险品装箱--map充当散列,记录数据出现次数
vector v[maxn]充当键值对

1. 原文

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 (≤104), 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:

1
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:

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
No
Yes
Yes

2. 解析

易燃液体对不能存放在一起。

1
vector<int> v[maxn]

记录他们之间的对应

对于每个集装箱建立

1
map<int,int> exist

将当前物品的所有匹配

1
exist[v[a][i]]++;

对于当前物品搜索exist[a]>0时,则该集装箱不合格

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<cstdio>
#include<map>
#include<vector>
using namespace std;
map<int,int> exist;
vector<int> v[100000];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a,b;
for (int i = 0; i < n; ++i)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
while(m--)
{
int k;
scanf("%d%d",&k,&a);
int flag=0;
for (int i = 1; i < k; ++i)
{
scanf("%d",&b);
if (exist[b]>0)
{
flag=1;
continue;
}
if(flag==0){
for (int j = 0; j < v[a].size(); ++j){
exist[v[a][j]]++;
}
}
a=b;
}
if (flag==0)
{
printf("Yes\n");
}else{
printf("No\n");
}
exist.clear();
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1149/
  • 发布时间: 2019-09-02 11:28
  • 更新时间: 2021-10-29 14:11
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!