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. AOP,过滤器,监听器,拦截器【转载】

    面向切面编程(AOP是Aspect Oriented Program的首字母缩写) ,我们知道,面向对象的特点是继承.多态和封装.而封装就要求将功能分散到不同的对象中去,这在软件设计中往往称为职责分配 ...

  2. HotSpot 虚拟机对象揭秘【转载】

    对象的创建 Java 对象的创建过程, ①类加载检查: 虚拟机遇到一条 new 指令时,首先将去检查这个指令的参数是否能在常量池中定位到这个类的符号引用,并且检查这个符号引用代表的类是否已被加载过.解 ...

  3. 获取url参数的方法(web)

    //获取url参数的方法(web) function GetQueryString(name) {     var reg = new RegExp("(^|&)" + n ...

  4. Ubuntu18.4中Apache在加不同端口的虚拟主机

    1.添加监听端口 sudo vim /etc/apache2/ports.conf Listen 80 Listen 6080 <IfModule ssl_module>         ...

  5. 《Inside C#》笔记(十三) 多线程 下

    一 任务调度 当一个线程的时间片被用尽后,处理器会切换到另一个线程,但关于如何确定执行哪一个线程呢,这就涉及到了线程或任务的优先级. a) 每个线程都有优先级,任务调度算法会根据各线程的不同优先级来决 ...

  6. Spark编译

    Spark的运行版本使用mvn编译,已经集成在源码中.如果机器有外网或者配置了http代理,可以直接调用编译命令来进行编译. windows&Linux命令如下: ./build/mvn \ ...

  7. python第六十一天,第六十二天 redis

    redis 缓存系统 redis是业界主流的key-value nosql 数据库之一.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).se ...

  8. Django日志信息路径的设置

    django日志信息路径的设置, 因为我们经常在代码业务上线时候 需要进行调试,查看代码的后台运行情况,就需要设置django项目的具体的日志信息运维的路径了 LOGGING = { 'version ...

  9. 为什么内核访问用户数据之前,要做access_ok?【转】

    linuxer 案例 比如内核的如下commit引入了一个严重的安全漏洞(编号CVE-2017-5123): 危害 一个攻击案例可以参考: freebuf <Linux内核Waitid系统调用本 ...

  10. fedora添加ntfs文件系统支持

    ntfs支持(安装后不能打开,重启) 如果没有换源先看一下换源. 查找库中是否有ntfs-3g. [root@bogon zhujikuan]# yum search ntfs 上次元数据过期检查:0 ...