其他分享
首页 > 其他分享> > L2-023 图着色问题

L2-023 图着色问题

作者:互联网

并不一定是连通图

#include <bits/stdc++.h>

using namespace std;

vector<int> g[510];

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

    vector<int> color(v + 1);
    vector<bool> have(v + 1);

    function<bool(int, int)> dfs = [&](int u, int c) {
        have[u] = true;
        for (auto& v : g[u]) {
            if (c == color[v]) return false;
            if (have[v]) continue;
            if (!dfs(v, color[v])) return false;
        }

        return true;
    };

    int n; cin >> n;
    for (int _ = 0; _ < n; _++) {
        fill(have.begin(), have.end(), false);
        set<int> S;
        for (int i = 1; i <= v; i++) {
            cin >> color[i];
            S.insert(color[i]);
        }
        if (S.size() != k) cout << "No" << "\n"[_ == n - 1];
        else {
            bool ok = true;
            for (int i = 1; i <= v; i++) {
                if (!have[i]) {
                    if (!dfs(i, color[i])) {
                        ok = false;
                        break;
                    }
                }
            }
            if (!ok) cout << "No" << "\n"[_ == n - 1];
            else cout << "Yes" << "\n"[_ == n - 1];
        }
    }

    return 0;
}

标签:false,int,cin,着色,color,vector,L2,023,return
来源: https://www.cnblogs.com/ZhengLijie/p/16034026.html