1090 危险品装箱 (25 分)
集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里。比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸。
本题给定一张不相容物品的清单,需要你检查每一张集装箱货品清单,判断它们是否能装在同一只箱子里。
输入格式:
输入第一行给出两个正整数:N (≤10
4
) 是成对的不相容物品的对数;M (≤100) 是集装箱货品清单的单数。
随后数据分两大块给出。第一块有 N 行,每行给出一对不相容的物品。第二块有 M 行,每行给出一箱货物的清单,格式如下:
K G[1] G[2] … G[K]
其中 K (≤1000) 是物品件数,G[i] 是物品的编号。简单起见,每件物品用一个 5 位数的编号代表。两个数字之间用空格分隔。
输出格式:
对每箱货物清单,判断是否可以安全运输。如果没有不相容物品,则在一行中输出 Yes,否则输出 No。
输入样例:
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
输出样例:
No
Yes
Yes
代码如下
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<string.h>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
const int max_n = 100006;
const int INF = 1000006;
vector<int>huchi[max_n];
int main()
{
int m, n,k;
int obj[1006];
memset(obj, -1, sizeof(obj));
scanf("%d %d", &n, &m);
int id1, id2;
for (int i = 0; i < n; i++)
{
scanf("%d %d", &id1, &id2);
huchi[id1].push_back(id2) ;
huchi[id2].push_back(id1);
}
for (int i = 0; i < m; i++)
{
scanf("%d", &k);
for (int j = 0; j < k; j++)
{
scanf("%d", &obj[j]);
}
bool yes = true;
for (int a = 0; a < k; a++)
{
for (int b = 0; b < k; b++)
{
for (int c = 0; c < huchi[obj[a]].size(); c++)
{
if (huchi[obj[a]][c] == obj[b])
{
yes = false;
printf("No\n");
break;
}
}
if (yes == false)//当时少了这个if
{
break;
}
}
if (yes == false)
{
break;
}
}
if (yes)
{
printf("Yes\n");
}
}
return 0;
}
总结
当时就是漏了一个if,导致没完全跳出循环,所以最后一个测试点出错了