PAT--1170 Safari Park
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.
解析:对于每对相邻动物园,我们可以先用map来记录,然后对于每种计划,我们初始化cnt[ ]来记录出现了几种动物,之后N^2直接枚举每一对动物,如果是同一种动物,就查询map判断(i,j)是否出现过,如果出现过则表示在同一个集合中,那么就是No,否则最后输出Yes即可。
注意:记录每一对动物园时,需要双向记录,测试点1就是针对这个点。
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N=510;
typedef pair<int,int> PII;
map<PII,int> mp;//记录每一对动物园
int k[N],cnt[N];
void solve()
{int n,r,m;scanf("%d%d%d",&n,&r,&m);while(r--){int a,b;scanf("%d%d",&a,&b);mp[{a,b}]=1;//双向记录!mp[{b,a}]=1;}int q;scanf("%d",&q);while(q--){int sum=0;//记录出现过几种动物memset(cnt,0,sizeof cnt);for(int i=1;i<=n;i++){scanf("%d",&k[i]);if(++cnt[k[i]]==1) sum++;//种类++}if(sum<m) printf("Error: Too few species.\n");else if(sum>m) printf("Error: Too many species.\n");else{bool flag=true;//是否合法for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if(k[i]==k[j]&&mp[{i,j}])//同种动物在同一集合中,不合法{flag=false;break;}}if(!flag) break;}if(flag) printf("Yes\n");else printf("No\n");}}
}
int main()
{solve();return 0;
}
PAT--1170 Safari Park相关推荐
- 2020年PAT甲级春季考试真题及答案
1168 Prime Day The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool "Pri ...
- 2020年9月PAT甲级满分必备刷题技巧
2020年7月的考试结束了,除了本次的考题更新,短期内不会更新. [7月题目的特点:首次线上考试,没出链表.树相关的模板题,第2到4题背景新颖,大大降低了抄袭历年代码的可能性,可以看作是线上考试的新趋 ...
- [PAT] 2020年春季赛题目 完整题目 + 完整答案 + 个人总结
[PAT] 2020年春季赛题目 完整题目 这次比赛在2020/07/25日举行, 距离比赛截止前5分钟. 100分的人数有: 86人 90分到99分的有: 142人 7-1 Prime Day (2 ...
- 泰迪杯C题第三问[文本有效性分析] (1)
导入库 import re # 正则表达式库 import collections # 词频统计库 import numpy as np # numpy数据处理库 import jieba # 中文分 ...
- problem solving automation
分工一定要明确啊啊啊啊啊 Nero 数据结构 图论 DP 博弈 JayYe 数论 DP 模拟搜索 博弈 wuyiqi 数论 数据结构 图论 字符串 模拟搜索 几何 20 ...
- 紫书《算法竞赛入门经典》
紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...
- 语料库(精华版)chapter 3/4/5
Chapter 3 雅思听力特别名词语料库 Chapter 4 雅思听力形容词/副词语料库 Chapter 5 雅思听力吞音/混合训练 Test Paper 1 Test Paper 2 Test P ...
- PAT题集2019.5.3排名变动
2019.5.3 团体天梯集 用户昵称 排名变动 总分变动 JS 116 -> 112 1696 -> 1711 ONE_FOR_ALL 276 -> 228 1446 -> ...
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
最新文章
- python input 数字_Python:raw_input读取数字的问题
- VS2005的Command Window 调试命令的总结(转载)
- 重载、重写(覆盖)、隐藏(重定义)
- android build获取ext,android – 如何在Gradle中获取当前构建类型
- java 远程查看电脑磁盘,请问你如何在Java中监视计算机的CPU、内存和磁盘使用情况?...
- UNION、UNION ALL和EXISTS
- 如何去掉Orcad Capture中元件标号下划线
- MFC的非模态对话框资源管理
- Jetpack来了:走近Google标准应用架构
- android文件恢复功能,终于找到了安卓手机删除的文件的恢复方法值得一看
- 开发人员的不断流动、让我们更加坚定信念,一定要控制好整个系统的底层架构、核心设计、日常质量检查工作
- hyperledger java_hyperledger fabric 1.4 使用java开发智能合约
- 在DW中实现鼠标移入字体变色
- jkd8 Stream的使用
- MySQL本地可以连接,远程连接不上的问题
- Hibernate高级映射技术(一)自定义数据类型StringList (转载用于收藏)
- Cesiumjs(一)原生cesiumjs集成进入vue框架
- XenServer 挂载其它硬盘及错误原因分析
- 用计算机浏览电子图片,电脑制作电子图片库手机上放有什么软件
- ios沙箱软件_iOS应用软件沙盒sandbox相关知识(整理)