1134 Vertex Cover (25分)

34 篇文章 0 订阅

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 10
​4
​​ ), 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:

N
​v
​​ v[1] v[2]⋯v[N
​v
​​ ]

where N
​v
​​ 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

用一个visit数组来存边,一开始输出边两边的点是,在结点信息下面存的是边的序号,第一条边为0。
遍历询问的num个结点,遍历这些结点的边们,将这些边的visit都++,然后遍历这个数组,如果有一条边为0,就输出no

#include<iostream>
#include<vector>
using namespace std;
vector<int> Adj[10010];//A[1]存的是1可连通的路径
int main(){
    int n,m;
    int u,v;
    cin>>n>>m;
    for(int i = 0; i < m; i++){
        cin>>u>>v;
        Adj[u].push_back(i);
        Adj[v].push_back(i);
    }
    int k,num;
    cin>>k;
    for(int i =0; i < k; i++){
        cin>>num;
        bool flag = true;
        vector<int> vertex(num+1);
        vector<int> visit(m+1,0);
        for(int j = 0; j < num; j++){
            cin>>vertex[j];
        }
        for(int j = 0; j < num; j++){
            for(int l = 0; l <Adj[vertex[j]].size(); l++){
                visit[Adj[vertex[j]][l]]++;
            }
        }
        for(int j = 0; j < m; j++){
            if(visit[j] == 0) flag = false;
        }
        if(flag == true) cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}

邻接表存储图,点之间记录的是路径序号,路径从0开始标号。遍历点集,然后遍历点下面的路径,将这条路径对应的值++。然后遍历路径数组,要是有路径没被遍历到,就输出no

//给你一组点,判断这些点是否包含了所有边,记录每条边两端的点,判断每条边是否全部能被访问到
//这张图每条边至少有一个点在点集里
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 10005;
int main(){
    int n,m;
    cin>>n>>m;
    vector<int> Adj[maxn];
    int u,v;
    for(int i = 0; i < m ; i++){
        cin>>u>>v;
        Adj[u].push_back(i);
        Adj[v].push_back(i);
    }
    int q,k,dian;
    cin>>q;
    for(int i = 0; i < q; i++){
        cin>>k;
        vector<int> v;
        for(int j = 0; j < k; j++){
            cin>>dian;
            v.push_back(dian);
        }
        int hashTable[maxn] = {0};//每条边是否被访问,被访问了就是1
        for(int j = 0; j < v.size(); j++){
            for(int l = 0; l < Adj[v[j]].size(); l++){
                hashTable[Adj[v[j]][l]]++;
            }
        }
        bool flag = true;
        for(int j = 0; j < m; j++){
            if(hashTable[j] == 0) flag = false;
        }
        if(flag == true) cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}

这道题倒是不错,每个点下面存的是边的序号。你问我为什么要存边的序号?你画下图就知道了,就算所有点都通过你的点集一次被访问到了,但是边不一定都被访问到了,所以点下面存边的序号。遍历点集下的所有点,将点下的边的hash值++,遍历hash表,如果有为0则no

//每条边至少有一个点在这个点集内,就是yes
//用邻接表存图,每个点下面存的是边的序号,遍历每个点下面的边将其hashTable++,遍历hashTable,如果有一条边的值为0就为no
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 10005;
vector<int> Adj[maxn];
int main(){
    int n,m;
    cin>>n>>m;
    int u,v;
    for(int i = 0; i < m; i++){
        cin>>u>>v;
        Adj[u].push_back(i);
        Adj[v].push_back(i);
    }
    int q,k,dian;
    cin>>q;
    for(int i = 0; i < q; i++){
        cin>>k;
        int hashTable[maxn] = {0};
        bool flag = true;
        for(int j = 0; j < k; j++){
            cin>>dian;
            for(int l = 0; l < Adj[dian].size(); l++){
                hashTable[Adj[dian][l]]++;
            }
        }
        for(int j = 0; j < m; j++){
            if(hashTable[j] == 0){
                flag = false;
                break;
            }
        }
        if(flag == true) cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值