PAT【甲级】1154

题目链接:PAT【甲级】1154
题目题目简述:给定几条由vertex vertes格式表示的边,再给定一组数字,表示节点的颜色。来让判断这个图是不是满足各条边的两端节点颜色都不同,如果满足则输出这个图总共使用了几种颜色,否则输出“No”。

#include<bits/stdc++.h>
using namespace std;

int v_color[10001];
unordered_map<int, pair<int, int>> graph;
unordered_map<int, bool> color_used;

int main(){
    int N, M, K;
    int c1, c2;
    cin >> N >> M;
    for (int i = 0; i < M;i++){
        cin >> c1 >> c2;
        graph[i] = pair<int, int>(c1, c2);
    }
    cin >> K;
    while(K--){
        int count_col = 0;
        color_used.clear();
        for (int i = 0; i < N;i++){
            cin >> v_color[i];
            if(!color_used[v_color[i]])
                count_col++;
            color_used[v_color[i]] = true;
        }
        bool flag = true;
        for (int i = 0; i < M;i++){
            if(v_color[graph[i].first] == v_color[graph[i].second]){
                flag = false;
                break;
            }
        }
        if(flag)
            cout << count_col << "-coloring" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

看清题意就简单了,就是去把每条边的两端节点判断一下颜色是否一样就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值