原题链接:暂无
关键词:图论、模拟
A safari park has K species of animals, and is divided into N regions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this is an NP complete problem, you are not expected to solve it. Instead, they have designed several distribution plans. Your job is to write a program to help them tell if a plan is feasible.
Input Specification:
Each input file contains one test case. For each case, the first line gives 3 integers: N
(0<N≤500), the number of regions; R
(≥0), the number of neighboring relations, and K
(0<K≤N), the number of species of animals. The regions and the species are both indexed from 1 to N.
Then R lines follow, each gives the indices of a pair of neighboring regions, separated by a space.
Finally there is a positive M
(≤20) followed by M lines of distribution plans. Each plan gives N
indices of species in a line (the i-th index is the animal in the i-th rigion), separated by spaces. It is guaranteed that any pair of neighboring regions must be different, and there is no duplicated neighboring relations.
Output Specification:
For each plan, print in a line Yes
if no animals in the two neighboring regions are the same, or No
otherwise. However, if the number of species given in a plan is not K, you must print Error: Too many species
. or Error: Too few species.
according to the case.
Sample Input:
6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
5
1 2 3 3 1 2
1 2 3 4 5 6
4 5 6 6 4 5
2 3 4 2 3 4
2 2 2 2 2 2
Sample Output:
Yes
Error: Too many species.
Yes
No
Error: Too few species.
题目大意:
有k种动物,和n个地区,r个地区之间的路。然后给出很多方案,问你是否满足k种动物,然后相邻的地区之间是不同的动物。
分析:
其实就是顶点着色问题:用k种颜色涂满n个顶点,然后相邻的顶点是不同的颜色。
代码:
// pat 7-3
#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
int g[maxn][maxn];
int main(){
//freopen("in.txt", "r", stdin);
int n, r, k;
cin >> n >> r >> k;
for(int i = 1; i <= r; i ++){
int a, b;
cin >> a >> b;
g[a][b] = g[b][a] = 1;
}
int m;
cin >> m;
for(int i = 1; i <= m; i ++){
vector<int> v(n+1);
unordered_set<int> se;
for(int j = 1; j <= n; j ++){
cin >> v[j];
se.insert(v[j]);
}
if(se.size() < k) puts("Error: Too few species.");
else if(se.size() > k) puts("Error: Too many species.");
else {
bool flag = 1;
for(int i = 1; i <= n; i ++){
for(int j = i; j <= n; j ++){
if(g[i][j] == 1 && v[i] == v[j]) flag = 0;
}
}
if(flag) puts("Yes");
else puts("No");
}
}
return 0;
}