首页 > 其他 > 详细

PAT_A1134#Vertex Cover

时间:2019-05-29 23:01:32      阅读:105      评论:0      收藏:0      [点我收藏+]

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:

N?v?? v[1v[2]?v[N?v??]

where N?v?? 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 #include<cstdio>
17 #include<algorithm>
18 const int M=1e4+10;
19 using namespace std;
20 struct node
21 {
22     int v1,v2;
23 }adj[M];
24 int vis[M];
25 
26 int main()
27 {
28 #ifdef    ONLINE_JUDGE
29 #else
30     freopen("Test.txt", "r", stdin);
31 #endif
32 
33     int n,m,k,v,v1,v2;
34     scanf("%d%d", &n,&m);
35     for(int i=0; i<m; i++)
36     {
37         scanf("%d%d", &v1,&v2);
38         adj[i].v1=v1;
39         adj[i].v2=v2;
40     }
41     scanf("%d", &k);
42     while(k--)
43     {
44         scanf("%d", &v);
45         fill(vis,vis+M,0);
46         for(int i=0; i<v; i++){
47             scanf("%d", &v1);
48             vis[v1]=1;
49         }
50         for(int i=0; i<m; i++){
51             if(vis[adj[i].v1]==0 && vis[adj[i].v2]==0){
52                     vis[n]=1;break;
53             }
54         }
55         if(vis[n])  printf("No\n");
56         else    printf("Yes\n");
57     }
58 
59     return 0;
60 }

 

PAT_A1134#Vertex Cover

原文:https://www.cnblogs.com/blue-lin/p/10946532.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!