PAT A1154 Vertex Coloring
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
-
思路 1:
邻接表存储图,DFS遍历,子代颜色如果与父亲相同return false, 遍历完没有发现不同return true,始终选用0作为起点(还是有缺陷的,有可能0不和其他点连通,但过了测试),总之,DFS还是太蠢了; -
TIPS 1:
找不同颜色数:不能使用hash表,因为题目给出的color只是不超过Int,有可能很大,开hash数组的化,要么超内存,要么初始化时间很长 --> set.size()来求颜色个数 -
code 1:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 11100;
int n, m, k;
int color[maxn];
bool flag[maxn];
vector<int> G[maxn];
set<int> col;
bool BFS(){
queue<int> q;
fill(flag, flag+maxn, false);
q.push(0);
flag[0] = true;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i = 0; i < G[now].size(); ++i){
int next = G[now][i];
if(color[next] == color[now]) return false;
if(flag[next] == false){
q.push(next);
flag[next] = true;
}
}
}
return true;
}
int main(){
scanf("%d %d", &n, &m);
int t1, t2, cnt;
for(int i = 0; i < m; ++i){
scanf("%d %d", &t1, &t2);
G[t1].push_back(t2);
G[t2].push_back(t1);
}
scanf("%d", &k);
while(k--){
unordered_set<int> col;
for(int i = 0; i < n; ++i){
scanf("%d", &color[i]);
col.insert(color[i]);
}
if(BFS() == false) printf("No\n");
else printf("%d-coloring\n", col.size());
}
return 0;
}
-
思路 2:
如果整个图,所有边的顶点颜色都不同,那就符合要求,想到遍历所有边,对于边的存储:1)可以使用vector存储边的结构体(包含两顶点) 2)使用multimap(因为有重复的key,不能用map) -
code 2-1:
#include <iostream>
#include <queue>
#include <map>
#include <unordered_set>
using namespace std;
const int maxn = 10010;
int n, m, k;
int color[maxn];
struct node{
int v1, v2;
};
vector<node> v(maxn);
int main(){
scanf("%d %d", &n, &m);
int t1, t2, cnt;
for(int i = 0; i < m; ++i){
scanf("%d %d", &v[i].v1, &v[i].v2);
}
scanf("%d", &k);
while(k--){
unordered_set<int> col; //!!!Wrong 1:set要放到里面,每轮都重置了!!
for(int i = 0; i < n; ++i){
scanf("%d", &color[i]);
col.insert(color[i]);
}
bool flag = true;
for(int i = 0; i < m; ++i){
if(color[v[i].v1] == color[v[i].v2]){
flag = false;
break;
}
}
if(flag) printf("%d-coloring\n", col.size());
else printf("No\n");
}
return 0;
}
- code 2-2
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int n, m, k;
int color[maxn];
multimap<int, int> mp;
int main(){
scanf("%d %d", &n, &m);
int t1, t2, cnt;
for(int i = 0; i < m; ++i){
scanf("%d %d", &t1, &t2);
mp.insert(make_pair(t1, t2));
}
scanf("%d", &k);
while(k--){
unordered_set<int> col; //!!!Wrong 1:set要放到里面,每轮都重置了!!
for(int i = 0; i < n; ++i){
scanf("%d", &color[i]);
col.insert(color[i]);
}
bool flag = true;
for(auto it = mp.begin(); it != mp.end(); ++it){
if(color[it->first] == color[it->second]){
flag = false;
break;
}
}
if(flag) printf("%d-coloring\n", col.size());
else printf("No\n");
}
return 0;
}
- T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
struct Edge{
int v1, v2;
}edge[maxn];
bool Judge(int ne, vector<int> & color){
for(int j = 0; j < ne; ++j){
if(color[edge[j].v1] == color[edge[j].v2]){
return false;
}
}
return true;
}
int main(){
int nv, ne;
scanf("%d %d", &nv, &ne);
for(int i = 0; i < ne; ++i){
scanf("%d %d", &edge[i].v1, &edge[i].v2);
}
int nq;
scanf("%d", &nq);
for(int i = 0; i < nq; ++i){
vector<int> color(nv);
unordered_set<int> st;
for(int j = 0; j < nv; ++j){
scanf("%d", &color[j]);
st.insert(color[j]);
}
if(Judge(ne, color)) printf("%d-coloring\n", st.size());
else printf("No\n");
}
return 0;
}