A1154 Vertex Coloring (25 point(s))

同一边两顶点颜色不同

1. 原文

A proper vertex coloring is a labeling of the graph’s vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at most k colors is called a (proper) k-coloring.

Now you are supposed to tell if a given coloring is a proper k-coloring.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 104), being the total numbers of vertices and edges, respectively. Then M lines follow, each describes an edge by giving the indices (from 0 to N−1) of the two ends of the edge.

After the graph, a positive integer K (≤ 100) is given, which is the number of colorings you are supposed to check. Then K lines follow, each contains N colors which are represented by non-negative integers in the range of int. The i-th color is the color of the i-th vertex.

output Specification:

For each coloring, print in a line k-coloring if it is a proper k-coloring for some positive k, or No if not.

Sample Input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
0 1 0 1 4 1 0 1 3 0
0 1 0 1 4 1 0 1 0 0
8 1 0 1 4 1 0 5 3 0
1 2 3 4 5 6 7 8 8 9

Sample output:

1
2
3
4
4-coloring
No
6-coloring
No

2. 解析

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<vector>
#include<cstdio>
#include<set>
using namespace std;
vector<int> v[10010];
int color[10010]={};
set<int> s;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a,b;
for (int i = 0; i < m; ++i)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
int k;
scanf("%d",&k);
while(k--){
int flag=0;
for (int i = 0; i < n; ++i)
{
scanf("%d",&color[i]);
s.insert(color[i]);
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < v[i].size(); ++j)
{
if (color[i]==color[v[i][j]])
{
flag=1;
break;
}
}
if (flag==1)
{
break;
}
}
if (flag==1)
{
printf("No\n");
}else{
printf("%lu-coloring\n",s.size());
}
s.clear();
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/01/A1154/
  • 发布时间: 2019-09-01 02:56
  • 更新时间: 2021-10-29 14:11
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!