思路:
利用 map+set 保存不兼容列表,然后查找,如果运载的货物有不能放到一起的,就输出 No, 否则输出 Yes
1149 Dangerous Goods Packaging (25 point(s))
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.
Example:
#include<iostream>
#include<vector>
#include<set>
#include<unordered_map>
using namespace std;
int main()
{
int N, M;
cin >> N >> M;
unordered_map<int, set<int>> goods;
goods.reserve(N);
for(int i = 1;i <= N; i++) {
int g1, g2;
cin >> g1 >> g2;
goods[g1].insert(g2);
goods[g2].insert(g1);
}
for(int i = 1;i <= M; i++) {
int K;
cin >> K;
vector<int> ship(K);
for(int j = 0; j < K; j++) cin >> ship[j];
bool Yes = true;
for(int j = 0; j < K; j++) {
if(goods.count(ship[j]) == 0) continue;
for(int h = j+1; h < K; h++) {
if(goods[ship[j]].count(ship[h])) {
Yes = false;
break;
}
if(!Yes) break;
}
if(!Yes) break;
}
cout << (Yes ? "Yes" : "No") << endl;
}
}