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[100001];
int inc[100001] = { 0 };
int req[110];
int main() {scanf("%d %d", &n, &m);for (int i = 0; i < n; i++) {int c1, c2;scanf("%d %d", &c1, &c2);adj[c1].insert(c2);adj[c2].insert(c1);inc[c1] = 1;inc[c2] = 1;}for (int i = 0; i < m; i++) {int flag = 0;scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &req[j]);}for (int j = 0; j < k; j++) {if (flag == 0) {for (int q = j + 1; q < k; q++) {if (adj[req[j]].find(req[q]) != adj[req[j]].end()) {flag = 1;break;}}}else break;}printf("%s\n", flag == 0 ? "Yes" : "No");}
}

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

转载于:https://www.cnblogs.com/tccbj/p/10412106.html

PAT A1149 Dangerous Goods Packaging (25 分)——set查找相关推荐

  1. 【PAT (Advanced Level) Practice】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 ——闲云潭影日悠悠,物换星移几度秋

    PAT A1149 Dangerous Goods Packaging 和单身狗那道题类似,不同的是,这里的狗都可以脚踏N只船(我最开始都没注意到,,) 所以首先多对多关系存入二维数组,然后维护一个标 ...

  3. PAT甲级1149 Dangerous Goods Packaging :[C++题解]哈希表、逆向思维

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 本题要反着想:先把所有询问存起来,每个询问存在一个hash表中.然后对于每对不相容的物品,查看是否同时在一个hash表中. ac代码 ...

  4. PAT乙级 1110 区块反转 (25 分) C++

    1110 区块反转 (25 分) 给定一个单链表 L,我们将每 K 个结点看成一个区块(链表最后若不足 K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转.例如:给定 L 为 1→2 ...

  5. 【PAT - 甲级1003】Emergency (25分)(Dijkstra,最短路条数,双权值最短路)

    题干: As an emergency rescue team leader of a city, you are given a special map of your country. The m ...

  6. PAT 1012 The Best Rank (25 分)

    1012 The Best Rank (25 分) 今天给大家分享的是PAT甲级的一道小题,设计题 原题请点击我 简单翻译: 设计一个排名表,这个表中以学生ID为主键,C表示程序设计语言的成绩,M表示 ...

  7. PAT 1060 Are They Equal (25 分)

    1060 Are They Equal (25 分) If a machine can save only 3 significant digits, the float numbers 12300 ...

  8. PAT乙级1030 完美数列 (25 分)

    1030 完美数列 (25 分) 给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可 ...

  9. PAT乙级1025反转链表 25(分)

    1025 反转链表 (25 分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为3→2→1→6→5→ ...

最新文章

  1. Python爬取近十万条程序员招聘数据,告诉你哪类人才和技能最受热捧! | 原力计划...
  2. 循环链表(约瑟夫环)的建立及C语言实现
  3. MariaDB 视图与触发器(11)
  4. vc获取当前路径,并且创建文件
  5. Android fingerprint指纹/face人脸代码导读
  6. 广州的11个辖区_广州“受欢迎”的2个区,相距仅20公里,若“合并”将超黄埔区...
  7. oracle导入del数据文件,pl/sql,Oracle数据库中,不小心delete数据并递交后如何恢复被删数据...
  8. JS中调用bignumber处理高精度小数运算
  9. Javascript调试之console对象,它的一些小技巧你都知道吗?
  10. MSSOAP与WebService
  11. 随想录(gcc生成的中间语言、汇编代码)
  12. 事物与持久化_DDD之聚合持久化应该怎么做?
  13. matplotlib常用绘图方法【转】
  14. 程序员的灯下黑:如果你想考研究生或是研究生
  15. mysql primary重复_mysql:键'PRIMARY'和奇怪的ID行为重复条目'0'
  16. R语言中写入Excel的不同sheet表格
  17. 【目录】pygame网络游戏教程
  18. 亲水性小分子PEG DBCO-PEG4-酸,1416711-60-8可以进行点击化学反应
  19. 亚马逊服务器怎么修改登录密码,将亚马逊aws的ec2服务器的登陆方式改为密码登陆...
  20. user-scalable=no 控制手机版浏览器网页分辨率

热门文章

  1. Java演示手机发送短信验证码功能实现
  2. centos7 和centos 6的一些区别
  3. Logistic回归 python实现
  4. 博弈论——关于Nim游戏和SG函数的几个链接
  5. Java -- 网络编程(一):Client与Server之间的数据传送
  6. Configuration、SessionFactory、Session
  7. dos下查看机器端口占用情况
  8. Spring Android 1.0.0.M3 发布
  9. 设置图片元素上下垂直居中的7种css样式_赵一鸣博客
  10. NodeJS+Express+Mysql+MongoDB之环境配置