题意:给出n对不能放在一起的货物,q个询问,每次询问一些货物是否能放在一起。
思路:用set存一下不能放在一起的货物,然后遍历即可。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
const int MAX_N = 10010;
int n, q, k, x, y, len, a[MAX_N];
set<pair<int, int>> st;
set<int> show;
void solve() {
for (int j = 0; j < len; j++) {
for (int t = j+1; t < len; t++) {
if (st.count(make_pair(a[j], a[t]))) {
printf("No\n"); return ;
}
}
}
printf("Yes\n"); return ;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++) {
scanf("%d %d", &x, &y);
st.insert(make_pair(x, y));
st.insert(make_pair(y, x));
show.insert(x); show.insert(y);
}
for (int i = 0; i < q; i++) {
scanf("%d", &k);
len = 0;
for (int j = 0; j < k; j++) {
scanf("%d", &x);
if (show.count(x)) a[len++] = x;
}
solve();
}
return 0;
}