【PAT甲级题解记录】1154 Vertex Coloring (25 分)

【PAT甲级题解记录】1154 Vertex Coloring (25 分)

前言

Problem: 1154 Vertex Coloring (25 分)

Tags:图的存储

Difficulty:剧情模式 想流点汗 想流点血 死而无憾

Address: 1154 Vertex Coloring (25 分)

问题描述

给定一个图的节点和边,以及每一个点的颜色,如果有临接点颜色相同输出No,否则输出不同的颜色数量。

解题思路

  • 方法一:遍历边即可,如果当成无向图遍历一遍会每个边多遍历一遍,但是时间足够就这么简单的写吧。

  • 方法二:除了直接遍历边,还可以用dfs来判断。

参考代码

  1. 遍历边
#include<iostream>
#include<vector>
#include<set>

using namespace std;
vector<vector<int>> edges;
vector<int> colors;
set<int> colors_num; // 统计颜色数量
int N, M, K;

void init() {
    cin >> N >> M;
    edges.resize(N);
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
}

void check() {
    bool proper = true;
    // 遍历边
    for (int i = 0; i < N && proper; ++i) {
        for (int j = 0; j < int(edges[i].size()) && proper; ++j) {
            if (colors[edges[i][j]] == colors[i]) proper = false;
        }
    }
    if (proper) {
        cout << colors_num.size() << "-coloring" << endl;
    } else {
        cout << "No" << endl;
    }
}

void solve() {
    // 初始化涂色
    colors.clear();
    colors.resize(N, 0);
    colors_num.clear();
    for (int i = 0; i < N; i++) {
        cin >> colors[i];
        colors_num.insert(colors[i]);
    }
    // 检查并输出
    check();
}

void solution_1154() {
    init();
    cin >> K;
    while (K--) {
        solve();
    }
}
int main() {
    solution_1154();
    return 0;
}
  1. DFS
#include<iostream>
#include<vector>
#include<set>

using namespace std;
vector<vector<int>> edges;
vector<int> visited;
vector<int> colors;
set<int> colors_num;
int N, M, K;
bool proper = true;

void dfs(int root) {
    if (!proper) return;
    visited[root] = 1;
    for (int i = 0; i < int(edges[root].size()); ++i) {
        int next_v = edges[root][i];
        if (colors[next_v] == colors[root]) {
            proper = false;
            return;
        }
        if (!visited[next_v]) {
            dfs(next_v);
        }
    }
}

void init() {
    cin >> N >> M;
    edges.resize(N);
    visited.resize(N, 0);
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
}

void solve() {
    colors.clear();
    colors.resize(N, 0);
    colors_num.clear();
    for (int i = 0; i < N; i++) {
        cin >> colors[i];
        colors_num.insert(colors[i]);
    }
    proper = true;
    visited.clear();
    visited.resize(N, 0);
    dfs(0);
    if (proper) {
        cout << colors_num.size() << "-coloring" << endl;
    } else {
        cout << "No" << endl;
    }
}

void solution_1154() {
    init();
    cin >> K;
    while (K--) {
        solve();
    }
}
int main() {
    solution_1154();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值