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 (≤104), 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
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
int n, m;
cin >> n >> m;
map<int, vector<int>> p;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
p[a].push_back(b);
p[b].push_back(a);
}
while (m--)
{
int k, flag = 0, book[100001] = { 0 };//第一次的程序把book定义在上面了,导致每次查询的时候用的一个book,从始至终都是一个book当然不行了
cin >> k;
vector<int> v(k);
for (int i = 0; i < k; i++)
{
cin >> v[i];
book[v[i]] = 1;//置为1,代表此次查询里有v[i]这个物品
}
for (int i = 0; i < k; i++)
for (int j = 0; j < p[v[i]].size(); j++)//遍历那些和v[i]不兼容的物品
if (book[p[v[i]][j]] == 1)//如果==1,说明与v[i]不兼容的也在此次查询中,那就要输出No了。
flag = 1;
printf("%s\n", flag ? "No" : "Yes");
}
return 0;
}
二刷:吾:
这个代码超时:用set中的find函数也不行,应该不可以借助数据结构和find函数,用种简单的方式实现才不会超时。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
const int maxn = 10006;
unordered_map<string, int> mp;
int main()
{
//freopen("in.txt", "r", stdin);
int n, m;
cin >> n >> m;
int index = 1;
vector<int> v[maxn];
for (int i = 0; i < n; i++)
{
string a, b;
cin >> a >> b;
if (mp[a]==0)
{
mp[a] = index;
index++;
}
if (mp[b]==0)
{
mp[b] = index;
index++;
}
v[mp[a]].push_back(mp[b]);
v[mp[b]].push_back(mp[a]);
}
for (int i = 0; i < m; i++)
{
int k;
cin >> k;
vector<int> list;
bool flag = true;
for (int j = 0; j < k; j++)
{
string good;
cin >> good;
list.push_back(mp[good]);
if (mp[good]==0)
{
continue;
}
for (int p = 0; p < v[mp[good]].size(); p++)
{
for (auto c : list)
{
if (c==v[mp[good]][p])
{
cout << "No" << endl;
flag = false;
break;
}
}
if (flag==false)
{
break;
}
}
}
if (flag)
{
cout << "Yes" << endl;
}
}
}
这个过了:
#include <iostream>
#include <vector>
#include <set>
#include <unordered_map>
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
int n, m,a,b;
cin >> n >> m;
unordered_map<int,vector<int>> mp;
for (int i = 0; i < n; i++)
{
cin >> a >> b;
mp[a].push_back(b);
mp[b].push_back(a);
}
for (int i = 0; i < m; i++)
{
int k, book[100006] = { 0 },flag=0;
cin >> k;
vector<int> list(k);
for (int j = 0; j < k; j++)
{
cin >> list[j];
book[list[j]] = 1;
}
for (int j = 0; j < k; j++)
{
for (int t = 0; t < mp[list[j]].size(); t++)
{
if (book[mp[list[j]][t]] == 1)
{
flag = 1;
}
}
}
printf("%s\n", flag == 1 ? "No" : "Yes");
}
return 0;
}