1170 Safari Park(39行代码+超详细注释)

分数 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.

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.

代码长度限制

16 KB

时间限制

300 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;
const int N=505;
int n,r,k;
int g[N][N],w[N];//g表示用邻接矩阵存储的图,w是结点的动物编号 
int flag=1;//0表示两个结点间出现动物编号相同,1则没有任何两条边的动物编号相同 
void func(){//若出现两条边之间的动物编号相同则flag置为0 
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(g[i][j]&&w[i]==w[j])flag=0;
}
int main(){
    cin>>n>>r>>k;
    for(int i=0;i<r;i++){//用邻接矩阵存储,输入 
        int a,b;
        cin>>a>>b;
        g[a][b]=g[b][a]=1;
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
        memset(w,0,sizeof w);//初始化w 
        flag=1;//初始化标记 
        map<int,int>mp;
        for(int j=1;j<=n;j++){//输入n个动物编号 
            int t;
            cin>>t;
            w[j]=t;//第j个结点的动物编号 
            mp[t]=1;//出现过的编号记录在mp中 
        }
        int cnt=mp.size();//即动物的数量 
        func();//遍历图 
        if(cnt>k)cout<<"Error: Too many species."<<endl;//若动物数量大于给定数量 
        else if(cnt<k)cout<<"Error: Too few species."<<endl;//小于 
        else if(flag==1)puts("Yes");//若满足要求 
        else puts("No"); //否则 
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值