1154 Vertex Coloring (25 分)(c++实现 已AC)

题目

题目链接: https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552
题目大意: 给定一个图, 以及图上的边, 任意一条边连接的点颜色不相同, 若成立, 则给出用了多少颜色. 捋清楚题目, 发现其实难度不大, 将边和节点颜色分别存下来, 遍历边检查边连接的节点是否合法, 同时用一个set记录使用的颜色. (记得每次上色后给出结果然后清空set. (妈呀, 这一个坑丢了25分, 也就考试的时候对IDE和键盘都有点懵)

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
using namespace std;
const int maxv = 10005;
const int maxe = 10005;
int n, m;
int colors[maxv];
struct edge {
    int v, w;
};
vector<edge> edges;
set<int> color_cnt;
int main() {
    scanf("%d%d", &n, &m);
    edge temp;
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &temp.v, &temp.w);
        edges.push_back(temp);
    }
    int k;
    scanf("%d", &k);
    while(k--) {
        bool flag = true;
        int max_color = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &colors[i]);
            color_cnt.insert(colors[i]);
        }
        for (auto e : edges) {
            if (colors[e.v] == colors[e.w]) {
                flag = false;
                break;
            }
        }
        if (flag) printf("%d-coloring\n", (int)color_cnt.size());
        else printf("No\n");
        color_cnt.clear();
    }
    return 0;
}

/*
// 调换顺序
10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
4
8 1 0 1 4 1 0 5 3 0
0 1 0 1 4 1 0 1 0 0
0 1 0 1 4 1 0 1 3 0
1 2 3 4 5 6 7 8 8 9
// 修改不相关节点颜色
10 1
8 7
1
0 0 0 0 0 0 0 0 0 0
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值