pta--1149 Dangerous Goods Packaging(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

【分析】

题意:给你若干组序列,每组表示互不相容的一组数。随后,几组测试。如果某组测试中有不相容的数对,则输出No,否则输出Yes;

思路:因为maxn是1e5+5,定义二维数组会爆的。所以,就用vector的二维。类似于,某两个点之间有边这种思想。然后,再开一个数组vis[],注意大小是maxn而不是1005!!!!在测试数据中,把该数对应的不相容数对标记,如果后面输入时出现,就flag=0即可。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
vector<int>v[maxn];
int vis[maxn];
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	while(m--)
	{
		memset(vis,0,sizeof(vis));
		int k,flag=1;
		scanf("%d",&k);
		for(int i=0;i<k;i++)
		{
			int x;
			scanf("%d",&x);
			if(!flag)continue;	
		//	cout<<vis[x]<<";;;\n";
			if(vis[x])
			{
				flag=0;
				continue;
			}
			for(int j=0;j<v[x].size();j++)
				vis[v[x][j]]=1;
		}
		if(flag)cout<<"Yes\n";
		else cout<<"No\n";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/82561411