zoukankan      html  css  js  c++  java
  • 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
    ", type);
            else puts("No");
        }
        
        return 0;
    }
    
  • 相关阅读:
    1015,存储过程,视图
    1009,数据库查询,聚合函数,日期时间函数
    1008,数据库表格创建,名称,格式

    公历和农历转换的JS代码
    面向对象之封装
    HTML之锚点
    HTML之css+div
    HTML基础
    SQL之定义变量
  • 原文地址:https://www.cnblogs.com/tomori/p/13754360.html
Copyright © 2011-2022 走看看