PAT(甲級)2020年春季考試 7-3 Safari Park

7-3 Safari Park (25分)

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.ios

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.ide

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.this

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.

題目限制:

image.png

題目大意:

一動物園有K種動物,被劃分到N個區域(K<N),每個區域都有一種動物(可重複),先給出每一個區域相鄰的關係和M個分配計劃,每個分配計劃給出,每個區域分配那個種類的動物,請問該計劃分配的動物種類數目是否爲K,若是多於K,輸出Error: Too many species.,若是小於K,輸出Error: Too few species. 。若是等於K,那麼判斷是否存在兩個相鄰的區域有着相同種類的動物,若是沒有,就輸出Yes,不然輸出No。spa

算法思路:

和以前的vertex coloring是一種類型的題目,其本質就是查詢是否存在某一條邊的兩個頂點,其分配的動物種類編號相同(顏色相同)。那麼咱們使用edges數組存儲全部的邊,map speciesPerRegion保存每個區域所分配的動物種類的映射,set species集合存儲每個計劃所用到的動物種類數目,根據其大小與K的關係進行輸出,在等於K的時候,遍歷每一條邊,判斷兩個頂點a和b是否出現speciesPerRegion[a]==speciesPerRegion[b],若是出現說明該計劃不符合要求,輸出No,不然輸出Yes。對於species大小大於K和小於K的時候就輸出相應信息便可。code

提交結果:

image.png

AC代碼:

#include<cstdio>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<string>
#include<iostream>

using namespace std;

struct Edge{
    int a,b;
};

int N,R,K;//頂點的個數,邊數,動物種類數目
vector<Edge> edges;

int main(){
    scanf("%d %d %d",&N,&R,&K);
    Edge edge;
    for (int i = 0; i < R; ++i) { 
        int a,b;
        scanf("%d %d",&edge.a,&edge.b);
        edges.push_back(edge);
    }
    int M;
    scanf("%d",&M);
    for (int j = 0; j < M; ++j) {
        unordered_set<int> species;//種類數目
        unordered_map<int,int> speciesPerRegion;//每個region的物種數目
        for (int i = 1; i <= N; ++i) {
            int num;
            scanf("%d",&num);
            species.insert(num);
            speciesPerRegion[i] = num;
        }
        if(species.size()>K){
            printf("Error: Too many species.\n");
        } else if(species.size()<K){
            printf("Error: Too few species.\n");
        } else {
            // 遍歷每一條邊
            bool isTrue = true;
            for(auto &item:edges){
                if(speciesPerRegion[item.a]==speciesPerRegion[item.b]){
                    isTrue = false;
                    break;
                }
            }
            if(isTrue){
                printf("Yes\n");
            } else {
                printf("No\n");
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索