A1134 Vertex Cover (25 point(s))

点序列包括所有边

1. 原文

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 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

1
2
3
4
5
No
Yes
Yes
No
No

2. 解析

点覆盖所有边 输入边a–> b时在对应点中存储边 v[a].push_back(i),v[b].push_back(i)

查询序列时 访问点的所有边信息visit[v[ a ] [ i ] ] =true;

查询所有边,存在边未被访问,则为false

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v[10010];
bool visit[10010]={false};
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a,b;
for (int i = 0; i < m; ++i)
{
scanf("%d%d",&a,&b);
v[a].push_back(i);
v[b].push_back(i);
}
int k;
scanf("%d",&k);
while(k--){
fill(visit,visit+10010,false);
int nv;
scanf("%d",&nv);
for (int i = 0; i < nv; ++i)
{
scanf("%d",&a);
for (int j = 0; j < v[a].size(); ++j)
{
visit[v[a][j]]=true;
}
}
int flag=0;
for (int i = 0; i < m; ++i)
{
if (visit[i]==false)
{
flag=1;
printf("No\n");
break;
}
}
if (flag==0)
{
printf("Yes\n");
}
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1134/
  • 发布时间: 2019-09-02 21:59
  • 更新时间: 2021-10-29 14:10
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!