1149 Dangerous Goods Packaging (25 point(s))

Ideas:

Use map+set to save the incompatibility list, and then search, if the carried goods cannot be put together, output No, otherwise output 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;
    }
}

 

Guess you like

Origin blog.csdn.net/u012571715/article/details/114700356