zoukankan      html  css  js  c++  java
  • A1134. Vertex Cover

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. Now given a graph with several vertex sets, you are supposed to tell if each of them is a vertex cover or not.

    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:

    Nv​​ v[1v[2]v[Nv​​]

    where Nv​​ is the number of vertices in the set, and v[i]'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

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int N, M, edge1[20001], edge2[20001], pt = 0;
    int hashV[20001] = {0};
    int main(){
        scanf("%d%d",&N, &M);
        for(int i = 0; i < M; i++){
            scanf("%d%d", &edge1[i], &edge2[i]);
        }
        int K;
        scanf("%d", &K);
        for(int i = 0; i < K; i++){
            int Nv, tag = 1;
            scanf("%d", &Nv);
            fill(hashV, hashV + N, 0);
            for(int j = 0; j < Nv; j++){
                int vv;
                scanf("%d", &vv);
                hashV[vv] = 1;
            }
            for(int j = 0; j < M; j++){
                if(hashV[edge1[j]] == 0 && hashV[edge2[j]] == 0){
                    tag = 0;
                    break;
                }
            }
            if(tag == 0)
                printf("No
    ");
            else printf("Yes
    ");
        }
        cin >> N;
        return 0;
    }
    View Code

    总结:

    1、题意:给出一个图,然后给出一组点的集合,问这组点能否满足:图中任意一条边均包含至少一个集合中的点。

    2、属于模拟题,不需要按照往常存储图的方法(邻接表、邻接矩阵)存储,只需要将每一条边存下来方便遍历即可。可按照edge1[N], edge2[N]的方式存储边。遍历时顺序遍历即可。查看点是否存在,可以将集合中的点都存在哈希表中。

     
  • 相关阅读:
    bzoj_auto_submiter(辣鸡Py毁我青春系列)
    听说“辣鸡小隔膜”出V1.3了?
    shell脚本:统计分析 /home/ 目录用户磁盘使用情况
    shell脚本:DNS自检脚本
    Linux命令集锦:ssh命令
    Linux用户权限
    Linux文件属性
    Linux命令集锦:chown命令
    Linux命令集锦:chmod命令
    Linux命令集锦:tmux命令
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/9553806.html
Copyright © 2011-2022 走看看