这个和甲级的一道题重复了。
今天真的不适合写代码吧,一个下午就ac了3道题,头很大。
这道题有点像leetcode朋友圈,应该可以用并查集做,我用了map的方法,具体看代码吧。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cctype>
#include <set>
#include <deque>
#include <cmath>
using namespace std;
unordered_map<int, set<int>> hash_table;
int main(){
// printf("123\n");
int n,m;
scanf("%d%d",&n,&m);
for (int i=0; i<n; i++) {
int a,b;
scanf("%d%d",&a,&b);
hash_table[a].insert(b);
hash_table[b].insert(a);
}
for (int i=0; i<m; i++) {
int a;
scanf("%d",&a);
vector<int> pack;
bool isIn[1000000] = { false };
for (int j=0; j<a; j++) {
int b;
scanf("%d",&b);
pack.push_back(b);
isIn[b] = true;
}
bool flag = true;
for (int j=0; j<a; j++) {
for (auto it =hash_table[pack[j]].begin(); it!=hash_table[pack[j]].end(); it++) {
if(isIn[*it]){
flag = false;
printf("No\n");
break;
}
}
if(!flag)
break;
}
if(flag)
printf("Yes\n");
}
}