PAT-A1149 Dangerous Goods Packaging (25分)

题意

给出已知的不能放在一起的物品,然后输入几组物品,判断是否合理。

思路

题目没什么难的,难在时间复杂度和空间复杂度之间的权衡,任何一方面把握不好就ac不了。我觉得我的思路挺好,所以分享一下。大致思路是准备一个充分大的数组,对于每一组,物品一个一个的放进去,同时也将其不兼容的物品全部放进这个数组,一旦放入新的物品时发现这个位置已经是 1,说明这个物品不能放进去。

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 "bits/stdc++.h"
using namespace std;
const int maxn = 100000;
int a[maxn];
int main(){
//     freopen("input.txt","r",stdin);
    int n,m; cin >> n >> m;
    unordered_map<int,vector<int>> map1;
    for (int i = 0; i < n; ++i) {
        int t1,t2; scanf("%d%d",&t1,&t2);
        map1[t1].push_back(t2); map1[t2].push_back(t1);
    }
    for (int i = 0; i < m; ++i) {
        int k; scanf("%d",&k);
        fill(a,a+maxn,0);
        bool no = false;
        for (int j = 0; j < k; ++j) {
            int id; scanf("%d",&id);
            if (a[id]) {
                no = true;
            } else if (map1.count(id)){
                for(int t:map1[id]) a[t] = 1;
            }
        }
        if (no) printf("No\n");
        else printf("Yes\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值