PAT (Advanced Level) Practice 1154 Vertex Coloring (25分) (直接存边遍历)
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:
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
2.题目分析
学死了…………一看到图就想到邻接矩阵,甚至想到DFS………………(无一例外的超时)
其实直接用vector存边遍历就行……
3.代码
1.正确代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<unordered_set>
using namespace std;
#define MAX 10001
int n, m, k;
int color[MAX];
struct node
{
int s;
int e;
};
int main()
{
int a, b;
scanf("%d %d", &n, &m);
vector<node>list;
for (int i = 0; i < m; i++)
{
scanf("%d %d", &a, &b);
list.push_back({ a,b });
}
cin >> k;
for (int i = 0; i < k; i++)
{
bool s = true;
unordered_set<int>com;
for (int j = 0; j < n; j++)
{
scanf("%d", &color[j]);
com.insert(color[j]);
}
for (int j = 0; j < list.size(); j++)
{
if (color[list[j].e] == color[list[j].s]) { s = false; break; }
}
if (s)printf("%d-coloring\n", com.size());
else printf("No\n");
}
}
2.超时代码
#include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
#include<unordered_set>
using namespace std;
#define INF 1000001
#define MAX 10001
int edges[MAX][MAX];
int n, m,k;
int visited[MAX];
int color[MAX];
bool BFS(int v)
{
bool success = true;
memset(visited, 0, sizeof(visited));
int w = 0;
queue<int>qu;
visited[v] = 1;
qu.push(v);
vector<int> list;
while (!qu.empty())
{
w = qu.front();
qu.pop();
list.push_back(w);
}
for (int j = 0; j < list.size(); j++)
{
w = list[j];
for (int i = 0; i <n; i++)
{
if (edges[w][i] != 0&&visited[i] == 0)
{
if (color[v] == color[i])success = false;
visited[i] = 1;
qu.push(i);
}
}
}
return success;
}
int main()
{
int a,b;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++)
{
scanf("%d %d", &a, &b);
edges[a][b] = 1;
edges[b][a] = 1;
}
cin >> k;
for (int i = 0; i < k; i++)
{
bool s = true;
unordered_set<int>com;
for (int j = 0; j < n; j++)
{
scanf("%d", &color[j]);
if (com.find(color[j]) == com.end())com.insert(color[j]);
}
for (int j = 0; j < n; j++)
{
if (!BFS(j)) { printf("No\n"); s = false; break; }
}
if (s)printf("%d-coloring\n",com.size());
}
}
3.超时代码2
#include<iostream>
#include<cstdio>
#include<unordered_set>
using namespace std;
#define MAX 10001
int edges[MAX][MAX];
int n, m, k;
int color[MAX];
int main()
{
int a, b;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++)
{
scanf("%d %d", &a, &b);
edges[a][b] = 1;
edges[b][a] = 1;
}
cin >> k;
for (int i = 0; i < k; i++)
{
bool s = true;
unordered_set<int>com;
for (int j = 0; j < n; j++)
{
scanf("%d", &color[j]);
if (com.find(color[j]) == com.end())com.insert(color[j]);
}
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
if (edges[j][k] == 1)
{
if (color[j] == color[k]) { s = false; break; }
}
}
if (!s)break;
}
if (s)printf("%d-coloring\n", com.size());
else printf("No\n");
}
}