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 ...
随机推荐
- Automating hybrid apps
Automating hybrid apps One of the core principles of Appium is that you shouldn’t have to change you ...
- Vue源码探究-状态初始化
Vue源码探究-状态初始化 Vue源码探究-源码文件组织 Vue源码探究-虚拟DOM的渲染 本篇代码位于vue/src/core/instance/state.js 继续随着核心类的初始化展开探索其他 ...
- 【bzoj2809】dispatching
这题的最优解法是可并堆,从上往下合并及删点,标准的O(nlogn)解法. 为了练习主席树,特用主席树写一发,可以按dfs序建立主席树,对每个子树进行查询. 总时间5232毫秒,要垫底了... 看来需要 ...
- Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心
题目链接:http://codeforces.com/contest/719/problem/B B. Anatoly and Cockroaches time limit per test 1 se ...
- 如何刷新本地的DNS缓存?
为了提高网站的访问速度,系统会在成功访问某网站后将该网站的域名.IP地址信息缓存到本地.下次访问该域名时直接通过IP进行访问.一些网站的域名没有变化,但IP地址发生变化,有可能因本地的DNS缓存没有刷 ...
- 步入C编程的第一天
我想学ruby以后开发网站,但ruby是高级语言,隐藏了许多底层的东西,因此先熟悉c语言 首先c程序的文件名是以.c结尾的 c程序的格式: 第一行#include<stdio.h> #是一 ...
- codeforces B. Multitasking 解题报告
题目链接:http://codeforces.com/problemset/problem/384/B 题目意思:给出n个数组,每个数组包括m个数字,当k = 0 时,需要把n个数组都按照从小到大的顺 ...
- 【转载】String和StringBuffer的区别,以及StringBuffer的常用方法介绍
String与StringBuffer的区别简单地说,就是一个变量和常量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象.Stri ...
- hadoop应用场景
大数据量存储:分布式存储 日志处理: Hadoop擅长这个 海量计算: 并行计算 ETL:数据抽取到oracle.mysql.DB2.mongdb及主流数据库 使用HBase做数据分析: 用扩展性应对 ...
- 怎样安装CentOS 6.6之三:磁盘分区的划分和修改
安装 CentOS(或Linux)系统,最难的就是磁盘分区.磁盘分区需要根据自己的实际使用需要来规划,以达到最优的效果. 工具/原料 CentOS 6.6 安装包 VMware 虚拟机 一.划分方 ...