当前位置: 代码迷 >> 综合 >> 1154 Vertex Coloring (25分)
  详细解决方案

1154 Vertex Coloring (25分)

热度:26   发布时间:2023-11-20 11:45:22.0

此题与图算法基本无关

存储所有边,存储所有顶点的颜色,枚举每一条边,判断该边两顶点颜色是否相同。

#include<iostream>
#include<set>
using namespace std;
const int maxn = 10010;
int n, m;
set<int>s;
struct {
    int vex1;int vex2;
}edges[maxn];
int colors[maxn];
int main() {
    cin >> n >> m;for (int i = 0; i < m; i++) {
    int x, y;cin >> x >> y;edges[i] = {
     x,y };}int c;cin >> c;for (int i = 0; i < c; i++) {
    s.clear();int no = 0;for (int j = 0; j < n; j++) {
    cin >> colors[j];s.insert(colors[j]);}for (int j = 0; j < m; j++) {
    if (colors[edges[j].vex1] == colors[edges[j].vex2]) {
    no = 1;break;}}if (no == 1)cout << "No" << endl;elsecout << s.size() << "-coloring" << endl;}return 0;
}