其他分享
首页 > 其他分享> > 1154 Vertex Coloring (25 分)

1154 Vertex Coloring (25 分)

作者:互联网

1154 Vertex Coloring (25 分)  

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 1), 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:

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:

4-coloring
No
6-coloring
No

着色,判断是否有两个连在一起的点用了同一颜色,没有就输出总颜色数量,否则输出No

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 int n, m, k;
 5 struct Node{
 6     int u,v;
 7 }node[10005];
 8 int val[10005], vis[10005];
 9 
10 bool dfs(){
11     for(int i = 0; i < m; i++){
12         if(val[node[i].u] == val[node[i].v])
13             return false;
14     }
15     return true;
16 }
17 
18 int main(){
19     cin >> n >> m;
20     int x,y;
21     for(int i = 0 ; i < m; ++i){
22         cin >> x >> y;
23         node[i].u = x;
24         node[i].v = y;
25     }
26     cin >> k;
27     while(k--){
28         map<int,int> mp;
29         int cnt = 0;
30         for(int i = 0; i < n; i++){
31             cin >> val[i];
32             if(mp[val[i]] == 0){
33                 mp[val[i]] = 1;
34                 cnt++;
35             }
36         }
37         if(dfs()){
38             cout <<cnt<<"-coloring"<<endl;
39         }else{
40             cout << "No" << endl;
41         }
42     }
43     return 0;
44 }

 


 
     

标签:node,Coloring,val,int,1154,Vertex,coloring,each,proper
来源: https://www.cnblogs.com/zllwxm123/p/11269346.html