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;
     } 
  • 相关阅读:
    栈和堆的详细介绍
    在DataTable中执行DataTable.Select("条件")返回DataTable;
    委托和事件
    面试宝典
    sql的寫法,推薦的寫法,全文索引提高類似like查詢的效率
    Google地图
    一般处理程序中,获取session
    提交表单
    手脱tElock 0.98b1 -> tE!
    手脱FSG 2.0 -> bart/xt
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/10622654.html
Copyright © 2011-2022 走看看