PAT basic level - Diamond like Dan 2-7-7 dangerous volume containers (25 minutes)

When the container transport of goods, we must be particularly careful not to incompatibility of goods packed in a box. Such as oxidants with flammable liquids must not be the same case, it will easily cause an explosion.

This question is incompatible given a list of items, you need to check each container a list of goods to determine whether they can be installed only in the same box.

Input formats:

Input of the first line gives two positive integers: N ( ≤) is the number of pairs of incompatibilities; M ( ≤) is a singular container inventory items.

Then divided into two blocks of data are given. The first one has  N rows, each given one pair of incompatible materials. A second block having  M rows, each row gives a list of goods packed in boxes, the following format:

K G[1] G[2] ... G[K]

Wherein  K ( ≤) is the number of pieces of the article, G[i] is the number of the article. For simplicity, each item with a 5-digit number represents. Two numbers separated by a space.

Output formats:

For a list of each package to determine whether it is safe to transport. If there is no incompatibility goods, the output in a row  Yes, otherwise the output  No.

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 <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
unordered_map<int,vector<int>> m;
bool no;
bool arr[100000];
bool contains(vector<int>& v){
    for(int i=0;i<v.size();i++)
        if(arr[v[i]]) return true;
    return false;
}
int main ()
{
    int N,M,P,tmp_int;
    int a,b;
    cin>>N>>M;
    while(N--){
        cin>>a>>b;
        m[a].push_back(b);
        m[b].push_back(a);
    }
    while(M--){
        cin>>P;
        fill(arr,arr+100000,false);
        while(P--){
            cin>>tmp_int;
            arr[tmp_int]=true;
        }
        no=false;
        for(int i=0;i<100000;i++)
            if(arr[i]&&contains(m[i]))//滿足第一個條件
                no=true;
        if(no) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11966814.html