L2-025 分而治之

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 10010, M = N * 2;

vector<int> g[N];

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b), g[b].push_back(a);
    }   

    int q;
    cin >> q;
    while (q--) {
        int k;
        cin >> k;
        set<int> destory;
        for (int i = 0; i < k; i++) {
            int x;
            cin >> x;
            destory.insert(x);
        }
        vector<bool> vis(n + 1, false); 

        function<void(int)> dfs = [&](int u) { 
            vis[u] = true;
            for (auto &v : g[u]) {
                if (vis[v] || destory.count(v)) continue;
                dfs(v);
            }
        };

        int res = 0;
        for (int i = 1; i <= n; i++) {
            if (!vis[i] && !destory.count(i)) {
                res++;
                dfs(i);
            }
        }

        if (res == n - k) cout << "YES" << "\n";
        else cout << "NO" << "\n";
    }

    return 0;
}
posted @ 2022-03-22 15:26  Xxaj5  阅读(22)  评论(0编辑  收藏  举报