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 N​v​​ 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 分)的更多相关文章

  1. PAT 甲级 1134 Vertex Cover

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...

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

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

  4. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  5. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  6. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

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

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

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

随机推荐

  1. 局域网如何通过SSH连接虚拟机装的centOS系统

    首先,在一个局域网内的一台机器上装了虚拟机,虚拟机上装了centos系统: 但是,只有本机能连接centos,其他电脑都连不上: ping了一下发现不通,然后排查原因. 我发现局域网内的机器IP都是: ...

  2. 火狐浏览器使用firebug获取xpath和css path

    工作中,常常会用到网页元素的定位方式,常用的有xpath和css path两种定位方式. 现在简单介绍如何使用工具自动生成元素的定位字符串. 首先介绍在火狐浏览器上使用FireBug及其扩展FireP ...

  3. UIView封装动画--iOS 利用系统提供方法来做弹性运动

    iOS 利用系统提供方法来做弹性运动 /*创建弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 velocity:弹性复位的速度 */ [UIView animateWith ...

  4. Python类的特殊属性

    Python中的特殊属性 定义如下类: class Foo(object): """Foo class definition""" 类的特殊 ...

  5. String StringBuffer StringBuilder 三者之间的区别

    今天被公司骗去面试,好糟心...... 这个问题不管去哪里面试,基础问题基本上都会问到这个问题.网上好多大神都总结,这里自己也总结一下. 首先我们最开始学的就是String字符串常量,这里有行代码 S ...

  6. 快速解决Android中的selinux权限问题【转】

    本文转载自:http://blog.csdn.net/mike8825/article/details/49428417 版权声明:本文为博主原创文章,未经博主允许不得转载. 关于selinux的详细 ...

  7. ios非UTF-8格式的网页解析

    网上有很多关于ios xml解析的方法,关于非UTF-8格式的网页解析也不少,我也试着看了好几个,但都没成功.今天无意中却弄好了,所以想和大家分享下.其实很简单,下面说下怎么得到非UTF-8格式的网页 ...

  8. .Net-Mongodb学习大全网址

    http://www.yuanjiaocheng.net/csharpmongo/16.html 介绍 在上一篇文章中,我们继续探索MongoDb .NET驱动程序中的数据序列化. 我们查看了各种属性 ...

  9. 双重检查锁实现单例(java)

    单例类在Java开发者中非常常用,但是它给初级开发者们造成了很多挑战.他们所面对的其中一个关键挑战是,怎样确保单例类的行为是单例?也就是说,无论任何原因,如何防止单例类有多个实例.在整个应用生命周期中 ...

  10. CodeForces 1109E. Sasha and a Very Easy Test

    题目简述:给定$m \leq 10^9+9$,维护以下操作 1. "1 l r x":将序列$a[l], a[l+1], \dots, a[r]$都乘上$x$. 2. " ...