zoukankan      html  css  js  c++  java
  • PAT-1134. Vertex Cover (25)

    1134. Vertex Cover (25)

    时间限制
    600 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 104), 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:

    Nv v[1] v[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
    

    提交代码

    这道题的关键是容易超时,自己写的代码仔细想想确实会超时,再参考了别人的代码,感觉妙哉。将每条边编号,记录每个顶点相关的边,将询问的顶点相关的边都标记了,再遍历所有的边,发现没有标记的说明没有全覆盖。

    也可以使用set容器,将所有询问顶点放入set中,遍历所有的边,判断set中是否有边的两个任意一个顶点,若没有则没有全覆盖。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int N, M, K;
    struct Node {
        int x;
        int y;
    };
    vector<Node> nodVec, nodVec1;
    
    void deleteNode(int x, vector<Node> nodV) {
    }
    
    int main()
    {
        cin>> N>> M;
        int x, y;
        for(int i = 0; i < M; i++) {//10^4
            scanf("%d%d", &x, &y);
            Node node;
            node.x = x;
            node.y = y;
            nodVec.push_back(node);
        }
        cin>> K;
        nodVec1 = nodVec;
        for(int i = 0; i < K; i++) {//10^2
            scanf("%d", &x);
            for(int j = 0; j < x; j++) {//10^4
                scanf("%d", &y);
                vector<Node>::iterator it = nodVec1.begin();
                while(it != nodVec1.end()) {//10^4每次都会遍历所有的边,虽然有erase,但是总量还是很大。
                    if(y == it->x || y == it->y) {
                        it = nodVec1.erase(it);
                    }
                    else {
                        it++;
                    }
                }
            }
            if(nodVec1.empty()) {
                printf("Yes
    ");
            }
            else {
                printf("No
    ");
            }
            nodVec1 = nodVec;
        }
        return 0;
    }
    #include<bits/stdc++.h>
    
    using namespace std;
    
    #define MAX 10004
    
    int N, M, K;
    vector<int> nodes[MAX];
    int flag[MAX];
    
    int main() {
        cin>> N>> M;
        int x, y;
        for(int i = 0; i < M; i++) {//10^4
            scanf("%d%d", &x, &y);
            nodes[x].push_back(i);
            nodes[y].push_back(i);
        }
        cin>> K;
        for(int i = 0; i < K; i++) {//10^2
            for(int j = 0; j < M; j++) {//10^4
                flag[j] = 0;
            }
            scanf("%d", &x);
            for(int j = 0; j < x; j++) {//10^4*2;这里每次遍历一个顶点的所有边,总共最多遍历两次所有的边
                scanf("%d", &y);
                vector<int>::iterator it;
                for(it = nodes[y].begin(); it != nodes[y].end(); it++) {
                    flag[*it] = 1;
                }
            }
            int flag2 = 1;
            for(int j = 0; j < M; j++) {
                if(flag[j] == 0) {
                    flag2 = 0;
                    printf("No
    ");
                    break;
                }
            }
            if(flag2 == 1) {
                printf("Yes
    ");
            }
        }
        return 0;
    }
    #include<bits/stdc++.h>
    
    using namespace std;
    
    int N, M, K;
    
    #define MAX 10004
    
    struct Node {
        int x, y;
    }nodes[MAX];
    
    set<int> nodeSet;
    
    int main() {
        cin>>N>>M;
        for(int i = 0; i < M; i++) {//10^4
            scanf("%d%d", &nodes[i].x, &nodes[i].y);
        }
        cin>>K;
        int x, y;
        for(int i = 0; i < K; i++) {//10^2
            cin>> x;
            nodeSet.clear();
            for(int j = 0; j < x; j++) {//10^4
                scanf("%d", &y);
                nodeSet.insert(y);
            }
            int flag = 1;
            for(int i = 0; i < M; i++) {//10^4*log10^4
                if(nodeSet.find(nodes[i].x) == nodeSet.end() && nodeSet.count(nodes[i].y) == 0) {
                    flag = 0;
                    break;
                }
            }
            if(!flag) {
                printf("No
    ");
            }
            else {
                printf("Yes
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    推荐一个采用方便程序员在线动画学习常用算法的良心网站
    你的ABAP程序给佛祖开过光么?来试试Jerry这个小技巧
    我在德国做SAP CRM One Order redesign工作的心得
    我做SAP CRM One Order redesign的一些心得体会
    一个最简单的WebSocket hello world demo
    推荐一个好用的以多tab标签方式打开windows CMD的工具
    SAP CX Upscale Commerce : SAP全新推出的电商云平台
    TCP socket和web socket的区别
    SAP 前端技术的演化史简介
    Fiori Fundamentals和SAP UI5 Web Components
  • 原文地址:https://www.cnblogs.com/ACMessi/p/8495610.html
Copyright © 2011-2022 走看看