Back to Ladder - L2-023 Figure Coloring Problem (25 points)

Topic description

The graph coloring problem is a well-known NP-complete problem. Given an undirected graph G=(V,E), can we assign a color to each vertex in V with K colors so that no two adjacent vertices have the same color?

But this question is not asking you to solve this coloring problem, but for a given color assignment, please judge whether this is a solution to the graph coloring problem.

Input format:

The input gives 3 integers V ( 0 < V ≤ 500 ) in the first row V (0 < V ≤ 500)V 0<V5 0 0 E (≥ 0) E (≥0)E0 K ( 0 < K ≤ V ) K(0<K≤V) K(0<KV ) , which are the number of vertices, edges, and colors of the undirected graph, respectively. Vertices and colors are both numbered from 1 to V. E lines follow, each giving the number of the two endpoints of an edge. After the information of the graph is given, a positive integerN (≤ 20) is given N (≤ 20)N20 ) is the number of color assignment schemes to be checked. After N lines, each line gives the color of V vertices in sequence (the ith number represents the color of the ith vertex), and the numbers are separated by spaces. The title guarantees that the given undirected graph is valid (ie, there are no self-loops and multiple edges).

Output format:

For each color assignment scheme, if it is a solution to the graph coloring problem, output Yes, otherwise, output No, each sentence occupies one line.

Input sample:

6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
4
1 2 3 3 1 2
4 5 6 6 4 5
1 2 3 4 5 6
2 3 4 2 3 4

Sample output:

Yes
Yes
No
No

Bipartite graph-coloring method

#include <iostream>
#include <cstring>
#include <set>
using namespace std;

const int N = 510;
int v, m, k, n;
int h[N], e[2*N*N], ne[2*N*N], idx;
int color[N];
bool st[N];

void add(int a, int b) {
    
    
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

bool dfs(int u) {
    
    
    
    // 遍历与该结点相连的结果,判断它们的颜色是否相同
    for (int i = h[u]; i != -1; i = ne[i]) {
    
    
        int j = e[i];
        if (!st[j]) {
    
    
            st[j] = true;
            if(!dfs(j)) return false;
            if (color[u] == color[j]) return false;
        }
        
    }
    return true;
}

int main() {
    
    
    memset(h, -1, sizeof h);
    cin >> v >> m >> k;
    
    for (int i = 1; i <= m; i++) {
    
    
        int a, b;
        cin >> a >> b;
        add(a, b), add(b, a); // 无向图
    }
    
    cin >> n;
    
    for (int i = 1; i <= n; i++) {
    
    
        memset(st, false, sizeof st); // 标记访问初始化
        set<int> s;
        for (int j = 1; j <= v; j++) {
    
    
            cin >> color[j];
            s.insert(color[j]);
        }
        if (s.size() != k) {
    
    
            cout << "No" << endl;
            continue;
        }
        bool flag = true;
        
        // 这里的图不一定是连通的,要全部单独遍历一遍
        // 当时染色法时没有掌握透彻,反思
        for (int j = 1; j <= v; j++) {
    
    
            if (!dfs(j)) flag = false;
        }
        
        if (!flag) cout << "No" << endl;
        else cout << "Yes" << endl;
        
    }

    return 0;
}

Return to the ladder-L2-023 Figure coloring problem (25 points)
Thanks to tzp for your help!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324139991&siteId=291194637