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 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 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
 
 1 #include <stdio.h>
 2 #include <string>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <string.h>
 7 using namespace std;
 8 const int maxn=10010;
 9 vector<int> adj[maxn];
10 //int g[maxn][maxn],sav[maxn][maxn];
11 int vis[maxn];
12 int n,m,k;
13 int main(){
14   scanf("%d %d",&n,&m);
15   for(int i=0;i<m;i++){
16     int c1,c2;
17     scanf("%d %d",&c1,&c2);
18     adj[c1].push_back(c2);
19     adj[c2].push_back(c1);
20     //g[c1][c2]=1;
21     //g[c2][c1]=1;
22   }
23   scanf("%d",&k);
24   while(k--){
25       int j;
26       scanf("%d",&j);
27       int cnt=0;
28       //memcpy(sav,g,sizeof(g));
29       fill(vis,vis+maxn,0);
30       for(int i=0;i<j;i++){
31           int v;
32           scanf("%d",&v);
33         vis[v]=1;
34           for(int q=0;q<adj[v].size();q++){
35               if(vis[adj[v][q]]==0){
36                   cnt++;
37             } 
38         }
39     }
40     if(cnt==m)printf("Yes\n");
41     else printf("No\n");
42   }
43 }
View Code

注意点:题目读了很久画出来才看懂,就是看给定的点集能不能包含这个图的所有边。

思路就是直接遍历一个点的所有边,把这个点的边条数记录下来,同时记录下这个点,后面有再包含这个边的不能重复计算,遍历完所有点边条数和输入时相等就是yes。

一开始想用二维数组,感觉判断会方便一些,结果又超时又超内存,10的四次方这个级别还是不能用邻接表实现。只有几百的时候可以用邻接表。