PAT甲级——1134 Vertex Cover (25 分)
1134 Vertex Cover (考察散列查找,比较水~)
我先在CSDN上发布的该文章,排版稍好https://blog.csdn.net/weixin_44385565/article/details/88897469
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 1), being the total numbers of vertices and the edges, respectively. Then M lines follow, each describes an edge by giving the indices (from 0 to N−1) of the two ends of the edge.
After the graph, a positive integer K (≤ 100) is given, which is the number of queries. Then K lines of queries follow, each in the format:

where Nv is the number of vertices in the set, and ['s are the indices of the vertices.
Output Specification:
For each query, print in a line Yes if the set is a vertex cover, or No if not.
Sample Input:
Sample Output:
No
Yes
Yes
No
No
题目大意:对于现有的图,给出K个顶点集合,判断这个集合是否为Vertex Cover;Vertex Cover对于每条边,至少有一个端点处于集合之中。
思路:题目还是比较水的,用结构数组存储边的两个端点,顶点集合用unordered_set存储,然后遍历图的边判断端点是否在集合之中。。c++的一些stl是真的非常好用,不用unordered_set的话也可以自己写一个哈希表(包括创建、插入和查找函数)。
下面是代码:
#include<iostream>
#include<unordered_set>
using namespace std;
struct node{
int v1,v2;//边的两个端点
};
int main(void)
{
int N,M,K;
scanf("%d%d",&N,&M);
node Edge[M];//用于存储边
for(int i=;i<M;i++)
scanf("%d%d",&Edge[i].v1,&Edge[i].v2);
scanf("%d",&K);
for(int i=;i<K;i++){
int Nv,tmp;
bool flag=true;
scanf("%d",&Nv);
unordered_set<int> se;//创建unordered_set集合,底层由哈希表实现
for(int j=;j<Nv;j++){
scanf("%d",&tmp);
se.insert(tmp);//将待判定的顶点存入集合
}
/*对于每条边,如果它的每个端点都在集合se中,则为Yes,否则就是No*/
for(int t=;t<M;t++){
if(se.find(Edge[t].v1)==se.end()&&se.find(Edge[t].v2)==se.end()){
flag=false;
break;
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return ;
}
PAT甲级——1134 Vertex Cover (25 分)的更多相关文章
- PAT 甲级 1134 Vertex Cover
https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...
- PAT Advanced 1134 Vertex Cover (25) [hash散列]
题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...
- PAT甲级——A1134 Vertex Cover【25】
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
随机推荐
- javascript学习第一天
从大学第一次接触到JavaScript,到现在一直是个软肋,之前也是学习过一遍,但是缺乏系统学习,基础不牢,那么今天开始从基础部分学起来,今天是第一天,每天至少要保证效率,也要保证学习质量. 恩,要按 ...
- ansible操作模块相关
1. 查看模块可用参数命令 ansible-doc -s module_name
- Django_model基础
Django-model基础 ORM 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 <------->类实例对象 创建表( ...
- Mongodb GridFS——适合大小超过16MB的文件
一.概述 GridFS是基于mongodb存储引擎是实现的“分布式文件系统”,底层基于mongodb存储机制,和其他本地文件系统相比,它具备大数据存储的多个优点.GridFS适合存储超过16MB的大型 ...
- DDD领域驱动之干货 (一)
说道DDD不得不说传统的架构与DDD的架构区别. 传统的架构不外乎就是三层,而在这三层里面又不断的细分,始终没有达到想要的效果,那么为什么当时还是采用三层. 当然在DDD没有提出的时候三层是大多数人的 ...
- 记录ubuntu16.04版本安装过程中遇到的问题
记录ubuntu16.04版本安装和使用过程中遇到的些问题,方便以后查看,主要内容有: 1. ubuntu源替换 2. windows与vmware ubuntu文件夹共享 3. putty连接ubu ...
- php获取YouTube视频信息的方法
YouTube的视频地址格式https://www.youtube.com/watch?v=[VIDEO_ID]例子:https://www.youtube.com/watch?v=psvkyf3Pz ...
- MongoDB分析工具之三:db.currentOp()
db.currentOp() db.currentOp是个好东西,顾名思义,就是当前的操作.在mongodb中可以查看当前数据库上此刻的操作语句信息,包括insert/query/update/rem ...
- leetcode 395 至少有K个重复字符的最长子串
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度. 示例 1: 输入: s = "aaabb", k = 3 输 ...
- 蓝桥杯 2014本科C++ B组 奇怪的分式 暴力枚举
蓝桥杯 枚举 奇怪的分式 标题:奇怪的分式 上小学的时候,小明经常自己发明新算法.一次,老师出的题目是: 1/4 乘以 8/5 小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1 ...