1154 Vertex Coloring (25分)

看见顶点涂色,还以为要dfs啥的,没想到就是简单模拟,散列表是手动模拟的没用unordered_map

#include<iostream>
#include<cstring>

using namespace std;

const int N = 10010, M = 20003;

struct{
    int a, b;
}edges[N];

int color[N];
int n, m;
int st[M], e[M], ne[M], idx;

void insert(int x){
    int k = x % M;
    e[idx] = x, ne[idx] = st[k], st[k] = idx ++;
}

int find(int x){
    int k = x % M;
    for(int i = st[k]; i != -1; i = ne[i]) if(e[i] == x) return 1;
    return 0;
}

int main(){
    cin >> n >> m;
    
    for(int i = 1; i <= m; i ++){
        int a, b;
        cin >> a >> b;
        
        edges[i] = {a, b};
    }
    
    int k;
    cin >> k;
    
    while(k --){
        int type = 0;
        memset(st, -1, sizeof st);
        idx = 0;
        for(int i = 0; i < n; i ++){
            int c;
            cin >> c;
            color[i] = c;
            if(!find(c)){
                insert(c);
                type ++;
            }
        }
        
        int res = 1;
        for(int i = 1; i <= m; i ++){
            int a = edges[i].a, b = edges[i].b;
            if(color[a] == color[b]){
                res = 0;
                break;
            }
        }
        
        if(res) printf("%d-coloring\n", type);
        else puts("No");
    }
    
    return 0;
}
posted @ 2020-09-30 14:25  yys_c  阅读(93)  评论(0编辑  收藏  举报