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 ...
随机推荐
- STM32 CAN通信
最近在STM32上开发CAN通信相关内容,转载一篇个人认为不错的文章,看完了基本算明白了,能够实际操作了. 原文地址: https://blog.csdn.net/ludaoyi88/article ...
- 负载均衡LVS
可参考:http://ixdba.blog.51cto.com/2895551/555738 http://os.51cto.com/art/201202/319979.html 也可以参考官网:ht ...
- finalize方法
什么是垃圾回收机制 不定时去堆内存中清理不可达对象.不可达的对象并不会马上就会直接回收, 垃圾收集器在一个Java程序中的执行是自动的,不能强制执行,即使程序员能明确地判断出有一块内存已经无用了,是应 ...
- 多线程设计模式(一) Single Threaded Execution
这里有一座独木桥.因为桥身非常的细,一次只能允许一个人通过.当这个人没有下桥,另一个人就不能过桥.如果桥上同时又两个人,桥就会因为无法承重而破碎而掉落河里. 这就是Single Threaded Ex ...
- 脚踏实地学C#5-扩展方法
扩展方法(Extension Method) MSDN定义:能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法须知: 1.扩展方法声明所在的类必须被声明为 ...
- WPF-初始屏幕(SplashScreen)
本主题介绍如何将启动窗口(也称为“初始屏幕”)添加到 Windows Presentation Foundation (WPF) 应用程序. 添加现有图像作为初始屏幕 创建或查找要用于初始屏幕的图像. ...
- css 鼠标移入边框填充效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- java-swing-JTextComponent
package com.http; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Swi ...
- MakeFile 文件的使用
什么是Makefile? 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译, ...