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. 具体CAS操作实现(无锁算法)

    具体CAS操作 上一篇讲述了CAS机制,这篇讲解CAS具体操作. 什么是悲观锁.乐观锁?在java语言里,总有一些名词看语义跟本不明白是啥玩意儿,也就总有部分面试官拿着这样的词来忽悠面试者,以此来找优 ...

  2. ssm简单整合(注释方法)

    1.创建web工程,选择web.xml文件,并导入相关jar包(使用的spring4.0以上版本,) 2.配置web.xml文件,包括spring.xml,springMVC.xml的加载映射,核心操 ...

  3. JPA与EJB3的关系

    JPA是基于Java持久化的解决方案,主要是为了解决ORM框架的差异,它的出现在某种程度上能够解决目前ORM框架之间不能够兼容的问题,对开发人员来说,能够更好的在JPA规范下进行系统开发. JPA全称 ...

  4. Linux 学习笔记之超详细基础linux命令(the end)

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...

  5. 喜闻乐见-Android简介

    本文主要是对Android系统做一个简介,包括其架构.启动流程.沙箱机制.APK.Darlvik以及ART. 1. 架构 Android是基于Linux内核开发出的一个移动操作系统,系统结构大致可以分 ...

  6. 用PRODUCT_COPY_FILES拷贝文件夹

    拷贝文件PRODUCT_COPY_FILES += device/qcom/msm8909/media/media_profiles_8909.xml:system/etc/media_profile ...

  7. python 遇到的小坑

    由于前端资源紧缺,我的后端系统迟迟等不来它的前端,没办法只好自己来写了.从html,js入门学起,然后照着vue.js的官方教程写了几个实例,从github上clone了一个不错的vue.js模版,填 ...

  8. 阿里云 centos7 django + uWSGI+Nginx + python3 部署攻略

    centos7+nginx+python3+django+uwsgi配置Django 项目部署   1.租的服务器(选择centos)的话,需要在阿里云后台控制台开放几个端口,克隆一下已开放的端口,t ...

  9. VsCode 的使用

    一.简介 VsCode(Visual Studio Code),官网地址:https://code.visualstudio.com/ Visual Studio Code is a lightwei ...

  10. 代理ARP--善意的欺骗

    1. 代理ARP(Proxy ARP)是什么? 顾名思义,它指通过中间设备(通常为Router)代替其他主机响应ARP请求.对于没有配置默认网关的主机想要与其他网络的另一台主机通信时,网关收到源主机的 ...