zoukankan      html  css  js  c++  java
  • 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
    ");
            else printf("No
    ");
        }
        return 0;
     } 
  • 相关阅读:
    Why Choose Jetty?
    Jetty 的工作原理以及与 Tomcat 的比较
    Tomcat设计模式
    Servlet 工作原理解析
    Tomcat 系统架构
    spring boot 打包方式 spring boot 整合mybaits REST services
    wireshark udp 序列号 User Datagram Protocol UDP
    Maven 的聚合(多模块)和 Parent 继承
    缓存策略 半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完全自动化的框架我更喜欢mybatis
    Mybatis解决sql中like通配符模糊匹配 构造方法覆盖 mybits 增删改
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/10622654.html
Copyright © 2011-2022 走看看