PAT甲级1149 Dangerous Goods Packaging (25分) 和乙级1090 危险品装箱 (25分) 尽力简化思路减少时间

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 (≤10
​4
​​ ), 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

这题给出的两两不匹配的货物ID ,所以不能用并查集,因为货物是两两不匹配的,比如20001 20003 可以共存的,但在并查集二者就是一个集合了,并不适用。

  • 题目也说给出的不匹配货物对 最多10^4 对,而且一个id可能对应几种不匹配的货物,感觉麻烦,所以还是把不匹配对保存起来,对后面给出的货物列表处理
  • 不匹配对子保存的话,每个id有个vector 或者set,都行,vector插入块查找满,set插入慢查找快,记录不匹配的货物。
  • 对于给出的货物列表,需要好好考虑,别使用每个货物id 和之前的货物的所有不匹配id进行比较,很慢,这样做的的话,其实第一个货物的不匹配集合被访问了好多次了,很慢 测试点2过不去的
  • 最好就是把货物列表记录中的id保存下来,使用sign[100000]={0} id存在就赋值为1,然后从头开始访问货物列表中每个元素的不匹配id,sign[id]==1就是有不匹配的,这样每个货物列表的不匹配货物id 就访问了一次,速度很快

代码


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <unordered_set>
using namespace std;

int main()
{
    int n,m;
    cin>>m>>n;


    vector<unordered_set<int>> mapp(100000);

    for(int i=0; i<m; i++)
    {
        int a,b;
        cin>>a>>b;
        mapp[a].insert(b);
        mapp[b].insert(a);
    }
    for(int i=0; i<n; i++)
    {
        int k;
        cin>>k;
        int sn[k];
        int has[100000]={0};
        for(int q=0; q<k; q++)
        {
            int ll;
            cin>>ll;
            has[ll]=1;//记录货物列表的id
            sn[q]=ll;
        }
        int out=1;
        for(int q=0; q<k&&out==1; q++)
        {
            int num=sn[q];
            for(auto  p=mapp[num].begin(); p!=mapp[num].end(); p++)
            {

                int maodun=*p;
                if(has[maodun]==1)
                {
                    out=0;
                    break;
                }
            }
        }
        if(out==1)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章