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
这道题之前我在刷乙级的时候做过,那时候还没学过STL,用C语言硬钢非常痛苦,一个多小时也没AC。这次用STL的map和set来做,就比较轻松了,大大减轻了思考的压力。
问题重述:
说有一些货物不能放到一起,这些不能同时放在一起的货物告诉你了,然后又给了一些清单,让我们检查一下是否合格。
解题思路:
利用map建立一个从string到set<string>的映射(注意一个货物可能和多个货物冲突),为什么用set而不用vector呢,因为set有一个find函数,可以直接查找比较方便,然后就可以读取了,读取的时候要注意双向添加。
然后就可以读取清单了,对清单进行检查。
首先贴上我原始的代码:
#include <iostream>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;
int n,m;
int main(){
cin>>n>>m;
map<string,set<string> > mp;
while(n--){
string a,b;
cin>>a>>b;
mp[a].insert(b);
mp[b].insert(a);
}
while(m--){
int k;
string t;
vector<string> shipp;
cin>>k;
while(k--){
cin>>t;
shipp.push_back(t);
}
int flag=1;
//遍历清单的每一件物品,从物品i开始,遍历i之后的每一件物品,获取物品i对应的set,即冲突物品集合,
//判断物品j是否存在于在这个集合中,只要存在一对,就证明冲突,直接退出。
for(int i=0;i<shipp.size();i++){
for(int j=i;j<shipp.size();j++){
if(mp[shipp[i]].find(shipp[j])!=mp[shipp[i]].end()){
flag=0;
break;
}
}
}
if(flag) cout<<"Yes\n";
else cout<<"No\n";
}
}
但是这样子后两个测试点会超时,所以我又想了一些其他的办法,比如map和set换成unordered的、cin换成scanf、int换成string等,但是依旧超时。
最后我分析,最耗费时间的其实是set查找的操作,为了减少查找次数,我这次换了一个检查清单的方式
- 用set1存储物品清单
- 遍历set1,获取set1清单上的每一个物品的冲突物品集合set2
- 遍历set2中的物品,判断该物品在清单集合set1中是否存在
- 如果存在,就说明存在冲突,直接退出。代码如下,这样子,就AC了!
#include <iostream>
#include <unordered_map>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
int n,m;
int main(){
cin>>n>>m;
unordered_map<int,unordered_set<int> > mp;
while(n--){
int a,b;
scanf("%d %d",&a,&b);
mp[a].insert(b);
mp[b].insert(a);
}
while(m--){
int k;
int t;
unordered_set<int> shipp;
scanf("%d",&k);
while(k--){
scanf("%d",&t);
shipp.insert(t);
}
int flag=1;
for(unordered_set<int>::iterator it = shipp.begin();it!=shipp.end();it++){
for(unordered_set<int>::iterator it2 = mp[*it].begin();it2!=mp[*it].end();it2++){
if(shipp.find(*it2)!=shipp.end()){
flag=0;
break;
}
if(flag==0) break;
}
}
if(flag) cout<<"Yes\n";
else cout<<"No\n";
}
}
AC啦!!!