https://pintia.cn/problem-sets/994805260223102976/problems/1038429484026175488
首选建立一个二维数组V(很大,以物品编号为一维下标),然后把每个不相容的物品放进去 比如按例子来说:
6 3
20001 20002
20003 20004
20005 20006
20003 20001
20005 20004
20004 20006
实现以20001为下标的V[20001]存放元素 20002 20003
即数组的第一维代表物品编号,扩展后的是与第一维不相容的物品编号
4 00001 20004 00002 20003
5 98823 20002 20003 20006 10010
3 12345 67890 23333
然后遍历查找4 00001 20004 00002 20003
就是以00001为下标查找 20004 00002 20003 是否在V[00001]里面
其他同理
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > data(100000);
int main(){
int N, M;
cin >> N >> M;
for(int i = 0; i < N; i++){
int tmp1, tmp2;
cin >> tmp1 >> tmp2;
data[tmp1].push_back(tmp2);
data[tmp2].push_back(tmp1);
}
for(int i = 0; i < M; i++){
int cnt, tmp[1005] = {0}, tmp2, flag = 0, sub = 0;
cin >> cnt;
for(int j = 0; j < cnt; j++)
cin >> tmp[j];
for(int j = 0; j < cnt; j++){
if(data[tmp[j]].size()){
for(auto i : data[tmp[j]]){
for(int k = j+1; k < cnt; k++){
if(i == tmp[k])
flag = 1;
}
}
if(flag) break;
}
if(flag) break;
}
if(flag){
cout << "No\n";
continue;
}else{
cout << "Yes\n";
continue;
}
}
return 0;
}