PAT乙级1090 危险品装箱 (25 分)

题目链接

思路

把每个货物与之不容的保存在一个vector中,然后对输入的货物清单中的货物进行遍历,看是否有在vector中的

实现

#include 
#include 
#include 
#include
using namespace std;


int main()
{
	int N, M, i,j;
	double temp1, temp2;
	cin >> N >> M;
	map<double, vector<double>> a, b;
	for (i = 0; i < N; i++)
	{
		cin >> temp1 >> temp2;
		a[temp1].push_back(temp2);
		a[temp2].push_back(temp1);
	}
	for (i = 0; i < M; i++)
	{
		int K,flag=1,gnum;
		cin >> K;
		vector<double> G;
		for (j = 0; j < K; j++)
		{
			cin >> gnum;
			if (a[gnum].size()==0)
				continue;
			G.push_back(gnum);
		}
			
		for (j = 0; j < G.size(); j++)
		{
			vector<double> ng = a[G[j]];
			for (int m = j; m < G.size(); m++)
			{
				if (find(ng.begin(), ng.end(), G[m]) != ng.end())
				{
					flag = 0;
					break;
				}
			}
			if (!flag)
				break;
		}
		if (!flag)
			cout << "No" << endl;
		if (flag)
			cout << "Yes" << endl;
	}
    return 0;
}


你可能感兴趣的:(PAT乙级)