当前位置: 首页 > article >正文

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

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个区域。管理人员希望将动物安排到所有区域,但相邻区域不能是同一种动物。当然,这是一个NP完全问题,不用去解决它。判断工作人员给出的方案是否可行。

分析:可以抽象为N个节点,R条边,边的两个端点的颜色不能相同。对于给出的方案,要判断:1、边的两个端点颜色是否相同,即值是否相同。2、出现的所有颜色是否恰好等于k。按照2个条件检查即可。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n,r,k,m;scanf("%d%d%d",&n,&r,&k);
    int edge[n+5][n+5];
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            edge[i][j]=0;
    for(int i=0;i<r;++i)
    {
        int a,b;scanf("%d%d",&a,&b);
        edge[a][b]=edge[b][a]=1;
    }
    scanf("%d",&m);
    while(m--)
    {
        int cnt=0,f=1;
        int ques[n+5]={0},flag[100000]={0};
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&ques[i]);
            if(flag[ques[i]]==0)flag[ques[i]]=1,cnt++;
            if(i>1&&f)
            {
                for(int j=1;j<i;++j)
                {
                    if(edge[i][j]&&ques[i]==ques[j])
                    {
                        f=0;break;
                    }
                }
            }
        }
        if(cnt>k)printf("Error: Too many species.\n");
        else if(cnt<k)printf("Error: Too few species.\n");
        else
        {
            if(f)printf("Yes\n");
            else printf("No\n");
        }
    }


    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

 


http://www.kler.cn/a/514226.html

相关文章:

  • AI守护煤矿安全生产:基于视频智能的煤矿管理系统架构解析
  • 基于微信小程序高校订餐系统的设计与开发ssm+论文源码调试讲解
  • Python----Python高级(正则表达式:语法规则,re库)
  • R语言基础| 回归分析
  • Spring MVC:设置响应
  • 2024.ailx10的年终总结
  • Unity预制体未即时刷新
  • 【SpringCloud】黑马微服务学习笔记
  • 备战春招—数字IC、FPGA笔试题(2)
  • Docker Load后存储的镜像及更改镜像存储目录的方法
  • Node.js 能做什么
  • 我的创作纪念日,纪念我的第512天
  • 【机器学习】量子机器学习:当量子计算遇上人工智能,颠覆即将来临?
  • 鸿蒙开发(32)arkTS、通过关系型数据库实现数据持久化封装
  • 鸿蒙系统的多端部署
  • 【漫话机器学习系列】052.解释平方和(Explained Sum of Squares, ESS)
  • Leetcode2218:从栈中取出 K 个硬币的最大面值和
  • 单片机基础模块学习——数码管
  • [Day 14]螺旋矩阵
  • 【深度学习】3.损失函数的作用
  • 【前端】HTML标签汇总
  • 微透镜阵列精准全检,白光干涉3D自动量测方案提效70%
  • rstrip 方法是 Python 字符串的一个内置方法,用于 删除字符串右边(末尾)的指定字符
  • WPF2-在xaml为对象的属性赋值
  • 大数据处理之数据去重、TopN统计与倒排索引的Hadoop实现
  • 关于在vue3中vue3-tree-org的简单应用