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

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <set>
using namespace std;
int n, m, k;
set<int> adj[];
int inc[] = { };
int req[];
int main() {
scanf("%d %d", &n, &m);
for (int i = ; i < n; i++) {
int c1, c2;
scanf("%d %d", &c1, &c2);
adj[c1].insert(c2);
adj[c2].insert(c1);
inc[c1] = ;
inc[c2] = ;
}
for (int i = ; i < m; i++) {
int flag = ;
scanf("%d", &k);
for (int j = ; j < k; j++) {
scanf("%d", &req[j]);
}
for (int j = ; j < k; j++) {
if (flag == ) {
for (int q = j + ; q < k; q++) {
if (adj[req[j]].find(req[q]) != adj[req[j]].end()) {
flag = ;
break;
}
} }
else break;
}
printf("%s\n", flag == ? "Yes" : "No");
}
}

注意点:看到题目第一眼想到的是图的联通块,但想想第二题应该不会考图,还是用map来做,用map好像有点麻烦,一个对应多个的时候不方便,那用vector,不好判断在不在里面。最终还是用set比较好,就开一个很大的set数组,然后一个个去查找就好了,内存和时间都不超。

PAT A1149 Dangerous Goods Packaging (25 分)——set查找的更多相关文章

  1. pat 1149 Dangerous Goods Packaging(25 分)

    1149 Dangerous Goods Packaging(25 分) When shipping goods with containers, we have to be careful not ...

  2. PAT_A1149#Dangerous Goods Packaging

    Source: PAT A1149 Dangerous Goods Packaging (25 分) Description: When shipping goods with containers, ...

  3. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  4. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  5. PAT A1142 Maximal Clique (25 分)——图

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  6. [PAT] 1142 Maximal Clique(25 分)

    1142 Maximal Clique(25 分) A clique is a subset of vertices of an undirected graph such that every tw ...

  7. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  8. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  9. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

随机推荐

  1. nginx重启服务

    修改完nginx配置后,需要使用 nginx -s reload使修改的配置生效,配置生效是平滑的,不会对访问产生任何影响reload后会启动新的进程接受新请求,对于未处理完的请求还是用老的配置,直到 ...

  2. node.js入门学习笔记整理

    (1)node Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node与javaScript的区别在于,javaScript的顶层对象是window,而no ...

  3. Chrome浏览器跨域

    配置新版Chrome浏览器跨域,需要创建用户数据文件夹,在其中保存浏览器的缓存.历史记录.收藏夹等数据. Windows系统Chrome跨域 1 下载Chrome 64位绿色版,解压缩,并在桌面创建快 ...

  4. tfs 禁止多人签出

    好久没用tfs了,忘了怎么设置了,记录下 编辑----->高级

  5. Android View体系(八)从源码解析View的layout和draw流程

    前言 上一篇文章我们讲了View的measure的流程,接下来我们讲下View的layout和draw流程,如果你理解了View的measure的流程,那这篇文章自然就不在话下了. 1.View的la ...

  6. Android--字符串和Drawable之间互相转化

    //将字符串转化成Drawable public synchronized static Drawable StringToDrawable(String icon) { if (icon == nu ...

  7. Git 学习一

    刚刚接触git,学习现骨干操作并记录一下过程中的小问题(Windows下) 1.新建git目录 创建一个目录,使用命令 git init 2.添加文件  git add a.txt 3.提交文件  g ...

  8. SpringBoot整合定时任务

    定时任务一般是项目中都需要用到的,可以用于定时处理一些特殊的任务. 在SpirngBoot中使用定时任务变的特别简单,不需要再像SpringMVC一样写很多的配置,只需要在启动类上增加一个@Enabl ...

  9. 编写一个BAT脚本协助运维人员遇到问题时候调测数据库是否有效连接成功的操作攻略

    简单摘要: 1.内网系统出现故障需要排查 2.运维人员不熟悉数据库操作,没法通过连接数据库和执行SQL语句的方式排查数据库及数据是否正常 3.解决方案:编写一个bat脚本,运维人员双击运行即可.   ...

  10. 5.1Python函数(一)

    目录 目录 前言 (一)函数的基本知识 (二)函数的基本使用 ==1.函数的简单定义== ==2.传值函数== (3)输出效果 ==3.不定长函数== ==4.缺省函数== ==5.函数的传值过程== ...