PAT_A1134#Vertex Cover

Source:

PAT A1134 Vertex Cover (25 分)

Description:

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 Mlines 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 ['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

Keys:

Attention:

  • vis[n]设为哨兵,少声明一个变量-,-

Code:

 1 /*
 2 Data: 2019-05-29 19:57:25
 3 Problem: PAT_A1134#Vertex Cover
 4 AC: 19:55
 5 
 6 题目大意:
 7 若集合中各顶点的边的集合 = 整个图的边集,称该顶点集为VC集;
 8 判断所给顶点集合是否为VC集
 9 输入:
10 第一行给出,顶点数N和边数M,均<=1e4
11 接下来M行,给出顶点对
12 接下来一行,给出测试数K<=100
13 接下来K行,首先给出顶点数N,接下来N个数表示N个顶点
14 
15 基本思路:
16 遍历各条边,若存在一条边的两个顶点均不在集合中,则非VC集
17 */
18 
19 #include<cstdio>
20 #include<algorithm>
21 const int M=1e4+10;
22 using namespace std;
23 struct node
24 {
25     int v1,v2;
26 }adj[M];
27 int vis[M];
28 
29 int main()
30 {
31 #ifdef    ONLINE_JUDGE
32 #else
33     freopen("Test.txt", "r", stdin);
34 #endif
35 
36     int n,m,k,v,v1,v2;
37     scanf("%d%d", &n,&m);
38     for(int i=0; i<m; i++)
39     {
40         scanf("%d%d", &v1,&v2);
41         adj[i].v1=v1;
42         adj[i].v2=v2;
43     }
44     scanf("%d", &k);
45     while(k--)
46     {
47         scanf("%d", &v);
48         fill(vis,vis+M,0);
49         for(int i=0; i<v; i++){
50             scanf("%d", &v1);
51             vis[v1]=1;
52         }
53         for(int i=0; i<m; i++){
54             if(vis[adj[i].v1]==0 && vis[adj[i].v2]==0){
55                     vis[n]=1;break;
56             }
57         }
58         if(vis[n])  printf("No\n");
59         else    printf("Yes\n");
60     }
61 
62     return 0;
63 }

 

 

posted @ 2019-05-29 21:54  林東雨  阅读(144)  评论(0编辑  收藏  举报