1149 Dangerous Goods Packaging (25 分)

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:

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<iostream>
#include<vector>
#include<map>
using namespace std;int main(){map<string,vector<string> > m1;map<string,int> m2;vector<string> v;int n,m;cin >> n >> m;string a,b;while(n--){cin >> a >> b;m1[a].push_back(b);m1[b].push_back(a);}int k;while(m--){cin >> k;m2.clear();v.clear();bool flag = 0;while(k--){cin >> a;v.push_back(a);m2[a] = 1;}for(int i = 0; i < v.size(); i++){for(int j = 0; j < m1[v[i]].size(); j++){if(m2[m1[v[i]][j]] == 1){flag = 1;break;}}}if(flag) cout << "No" <<endl;else cout << "Yes" <<endl;}return 0;
}

 

转载于:https://www.cnblogs.com/wanghao-boke/p/10458780.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385114.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《数据结构与算法》

序号内容1排序算法概念2快速排序算法3堆排序算法4归并排序算法

一个类的指针指向NULL去访问该类的成员函数

对象指针为NULL&#xff0c;为什么还是可以调用成员函数

1091 N-自守数 (15 分)

如果某个数 K 的平方乘以 N 以后&#xff0c;结果的末尾几位数等于 K&#xff0c;那么就称这个数为“N-自守数”。例如 392​2​​25392&#xff0c;而 25392 的末尾两位正好是 92&#xff0c;所以 92 是一个 3-自守数。 本题就请你编写程序判断一个给定的数字是否关于某个 N 是…

1092 最好吃的月饼 (20 分

月饼是久负盛名的中国传统糕点之一&#xff0c;自唐朝以来&#xff0c;已经发展出几百品种。 若想评比出一种“最好吃”的月饼&#xff0c;那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话&#xff0c;给出全国各地各种月饼的销量&#xff0c;要求你从中找出销量冠军…

86. 分隔链表

给定一个链表和一个特定值 x&#xff0c;对链表进行分隔&#xff0c;使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 示例: 输入: head 1->4->3->2->5->2, x 3 输出: 1->2->2->4->3->5 来…

1094 谷歌的招聘 (20 分)

2004 年 7 月&#xff0c;谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌&#xff08;如下图&#xff09;用于招聘。内容超级简单&#xff0c;就是一个以 .com 结尾的网址&#xff0c;而前面的网址是一个 10 位素数&#xff0c;这个素数是自然常数 e 中最早出现的 10 位连续…

Observer观察者模式

#include <iostream> #include <list> using namespace std;class Observer;class Subject { //抽象被观察者 public:virtual void Attach(Observer*) 0;virtual void Detach(Observer*) 0;virtual void Notify() 0;virtual int GetState() { return state_; }v…

1093 字符串A+B (20 分)

给定两个字符串 A 和 B&#xff0c;本题要求你输出 AB&#xff0c;即两个字符串的并集。要求先输出 A&#xff0c;再输出 B&#xff0c;但重复的字符必须被剔除。 输入格式&#xff1a; 输入在两行中分别给出 A 和 B&#xff0c;均为长度不超过 10​6​​的、由可见 ASCII 字符…

1095 解码PAT准考证 (25 分)

PAT 准考证号由 4 部分组成&#xff1a; 第 1 位是级别&#xff0c;即 T 代表顶级&#xff1b;A 代表甲级&#xff1b;B 代表乙级&#xff1b;第 2~4 位是考场编号&#xff0c;范围从 101 到 999&#xff1b;第 5~10 位是考试日期&#xff0c;格式为年、月、日顺次各占 2 位&am…

《基础算法》

序号内容1工厂模式2观察者模式3单例模式4策略模式5memcpy、memmove6strpcy7strcat8strcmp9快速排序算法10归并排序算法11堆排序算法

C++ STL之Vector

向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器。 vector 属于STL(Standard Template Library, 标准模板库)中的一种自定义的数据类型, 可以广义上认为是数组的增强版。vector是C用于数组类的容器&#xff0c;它可以被近似的认为是一个自动增长…

字符流中第一个不重复的字符

题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如&#xff0c;当从字符流中只读出前两个字符"go"时&#xff0c;第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时&#xff0c;第一个只出现一次的字符是&q…

C++ STL之Set

set是关联容器&#xff0c;类似于集合。 特点是里面的元素不会重复&#xff0c;而且元素时有序的。 1.声明定义&#xff1a; #include<set>using namespace std;set<int> s; 2.常见用法 s.inert(5); //插入 s.begin(); //返回s的第一个元素 s.end(); // 返回最后一…

表示数值的字符串

题目描述 请实现一个函数用来判断字符串是否表示数值&#xff08;包括整数和小数&#xff09;。例如&#xff0c;字符串"100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","…

数据库简单实用(一)

一、实验环境&#xff1a; MySQL 2.7.22 xfce终端 二、开发准备 打开MySQL服务并使用root登录 sudo service mysql start //打开mysql服务 mysql -u root //使用root用户登录&#xff0c;密码为空 -u和root之间可以不用空格 三、实验步骤 //如果在操作中忘掉&#xff1b;可以使…

lambda 表达式定制操作

泛型算法中的定制操作 许多算法都会比较输入序列中的元素以达到排序的效果&#xff0c;通过定制比较操作&#xff0c;可以控制算法按照编程者的意图工作。 普通排序算法&#xff1a;template<class RandomIterator> void sort(RandomIterator first,RandomIterator last)…

包含min函数的栈

题目描述 定义栈的数据结构&#xff0c;请在该类型中实现一个能够得到栈中所含最小元素的min函数&#xff08;时间复杂度应为O&#xff08;1&#xff09;&#xff09;。 解法&#xff1a; class Solution { public:stack<int> s;stack<int> s_min;void push(int v…

01-复杂度3 二分查找 (20 分)

本题要求实现二分查找算法。 函数接口定义&#xff1a; Position BinarySearch( List L, ElementType X );其中List结构定义如下&#xff1a; typedef int Position; typedef struct LNode *List; struct LNode {ElementType Data[MAXSIZE];Position Last; /* 保存线性表中最后…

二叉树的镜像

题目描述 操作给定的二叉树&#xff0c;将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义&#xff1a;源二叉树 8/ \6 10/ \ / \5 7 9 11镜像二叉树8/ \10 6/ \ / \11 9 7 5 解法&#xff1a; /* struct TreeNode {int val;struct TreeNode *left;struct Tre…

01-复杂度1 最大子列和问题 (20 分)

给定K个整数组成的序列{ N​1​​, N​2​​, ..., N​K​​ }&#xff0c;“连续子列”被定义为{ N​i​​, N​i1​​, ..., N​j​​ }&#xff0c;其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 }&#x…