1149 Dangerous Goods Packaging (25 point(s))

思路:

利用 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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值