题目:https://pintia.cn/problem-sets/994805260223102976/problems/1038429484026175488

代码

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<map>
  4. #include<vector>
  5. using namespace std;
  6. map<int, vector<int> > mp;
  7. void new_and_append(int temp_a, int temp_b){
  8. if(mp.find(temp_a) == mp.end()){
  9. vector<int> temp_vec;
  10. temp_vec.push_back(temp_b);
  11. mp[temp_a] = temp_vec;
  12. } else {
  13. mp[temp_a].push_back(temp_b);
  14. }
  15. }
  16. int main(){
  17. int n, m;
  18. int temp_a, temp_b;
  19. scanf("%d %d", &n, &m);
  20. for(int i = 0; i < n; i++){
  21. scanf("%d%d", &temp_a, &temp_b);
  22. new_and_append(temp_a, temp_b);
  23. new_and_append(temp_b, temp_a);
  24. }
  25. int turns, temp_int;
  26. for(int i = 0; i < m; i++){
  27. scanf("%d", &turns);
  28. int flag = 0;
  29. vector<bool> hash(100010, false);
  30. for(int j = 0; j < turns; j++){
  31. scanf("%d", &temp_int);
  32. hash[temp_int] = true;
  33. for(int k = 0; k < mp[temp_int].size(); k++){
  34. if(hash[mp[temp_int][k]]){
  35. flag = 1;
  36. break;
  37. }
  38. }
  39. }
  40. flag == 0 ? printf("Yes\n") : printf("No\n");
  41. }
  42. }