当前位置: 首页 > news >正文

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 N1) 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:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
5
4 0 3 8 4
6 6 1 7 5 4 9
3 1 8 4
2 2 8
7 9 8 7 6 5 4 2

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=0;i<M;i++)
        scanf("%d%d",&Edge[i].v1,&Edge[i].v2);
    scanf("%d",&K);
    for(int i=0;i<K;i++){
        int Nv,tmp;
        bool flag=true;
        scanf("%d",&Nv);
        unordered_set<int> se;//创建unordered_set集合,底层由哈希表实现 
        for(int j=0;j<Nv;j++){
            scanf("%d",&tmp);
            se.insert(tmp);//将待判定的顶点存入集合 
        }
        /*对于每条边,如果它的每个端点都在集合se中,则为Yes,否则就是No*/
        for(int t=0;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 0;
 } 

转载于:https://www.cnblogs.com/yinhao-ing/p/10622654.html

相关文章:

  • 控制特效在UI上面
  • python-opencv学习第一章
  • Hive启动失败
  • kajiya-kay 头发
  • Prncnfg.vbs参数详解
  • Docker安装
  • Precomputed Radiance Transfer for Real-Time Rendering in Dynamic, Low-Frequency Lighting Environment
  • Ocelot-基于.NET Core的开源网关实现
  • Real-Time Rendering——Chapter 6 Texturing
  • 边缘分析及直线和圆拟合——第5讲
  • Towards Real-Time Photorealistic Rendering: Challenges and Solutions
  • SpringBoot——定时任务+WebSocket(问题)
  • unity调试lua工具和方法——IntelliJ IDEA+EmmyLua
  • cad.net 更改高版本填充交互方式为低版本样子
  • xlua——03_UIEvent学习
  • gitlab-ci配置详解(一)
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • Mysql数据库的条件查询语句
  • rc-form之最单纯情况
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • Yii源码解读-服务定位器(Service Locator)
  • 百度地图API标注+时间轴组件
  • 关于Java中分层中遇到的一些问题
  • 关于springcloud Gateway中的限流
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 前端面试总结(at, md)
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 我的zsh配置, 2019最新方案
  • 在Mac OS X上安装 Ruby运行环境
  • 06-01 点餐小程序前台界面搭建
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • #define、const、typedef的差别
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (1)虚拟机的安装与使用,linux系统安装
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (33)STM32——485实验笔记
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (办公)springboot配置aop处理请求.
  • (分类)KNN算法- 参数调优
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (算法设计与分析)第一章算法概述-习题
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • (转)人的集合论——移山之道
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .form文件_一篇文章学会文件上传
  • .net 反编译_.net反编译的相关问题
  • .net访问oracle数据库性能问题
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • .net知识和学习方法系列(二十一)CLR-枚举
  • .secret勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
  • @AliasFor注解