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

 #include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
const int maxn=;
vector<int> adj[maxn];
//int g[maxn][maxn],sav[maxn][maxn];
int vis[maxn];
int n,m,k;
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<m;i++){
int c1,c2;
scanf("%d %d",&c1,&c2);
adj[c1].push_back(c2);
adj[c2].push_back(c1);
//g[c1][c2]=1;
//g[c2][c1]=1;
}
scanf("%d",&k);
while(k--){
int j;
scanf("%d",&j);
int cnt=;
//memcpy(sav,g,sizeof(g));
fill(vis,vis+maxn,);
for(int i=;i<j;i++){
int v;
scanf("%d",&v);
vis[v]=;
for(int q=;q<adj[v].size();q++){
if(vis[adj[v][q]]==){
cnt++;
}
}
}
if(cnt==m)printf("Yes\n");
else printf("No\n");
}
}

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

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

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

PAT A1134 Vertex Cover (25 分)——图遍历的更多相关文章

  1. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  2. PAT甲级——A1134 Vertex Cover【25】

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  3. PAT Advanced 1134 Vertex Cover (25) [hash散列]

    题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...

  4. A1134. Vertex Cover

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  5. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  6. PAT 1134 Vertex Cover

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  7. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  8. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  9. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

随机推荐

  1. 设计模式之访问者模式(Visitor )

    访问者模式是一种将数据操作和数据结构分离的设计模式,可以说是面向数据密集型的一种设计方式,数据的结构相对稳定,有明显的分层和分类,而对数据对象的相关操作进行分组.分析等二次加工,这些操作都是由访问者来 ...

  2. Harbor 搜索镜像及查看 tag

    在我们搭建完 Harbor 后: https://www.cnblogs.com/klvchen/p/9482153.html 如果想要通过 API 获取 Harbor 上面的镜像及 tag 可以使用 ...

  3. 纯css抖动效果

    HTML: <button class="shake">按钮</button> CSS: .shake{ width: 120px; height: 33p ...

  4. layer.open

    1.type-基本层类型 类型:Number,默认:0 layer提供了5种层类型.可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层). 若你采用layer. ...

  5. ionic3打包不能prod的问题

    在最近的项目中,我ionic3采用了懒加载,来提高性能.但是当我普通打包的时候,正常成功了,但是加上--prod的时候,就报错了. 报错如下: 大概意思就是page是声明的一部分,然后请在更高级声明之 ...

  6. CSS--居中方式总结

    一.水平居中方法 1.行内元素.字体的水平居中 1.对于行内元素(display值为inline或inline-block都可以)或者字体:父元素添加css规则:text-align:center; ...

  7. CentOS7 下源码安装 python3

    CentOS 7 下源码安装 python3   在CentOS7下,默认安装的是python2.7:为满足项目要求,安装python3 的方法如下:   1. 首先安装python3.6可能使用的依 ...

  8. 基于Grafana的监控数据钻取功能应用实践

    互联网企业中,随着机器规模以及业务量的爆发式增长,监控数据逐渐成为一种大数据,对监控大数据的分析,包括数据采集.数据缓存.数据聚合分析.数据存储.数据展现等几个阶段.不同阶段有不同的解决方案及支撑工具 ...

  9. OkHttp3源码详解(二) 整体流程

    1.简单使用 同步: @Override public Response execute() throws IOException { synchronized (this) { if (execut ...

  10. [iOS] WSHorizontalPickerView 图片水平滚动封装

    之前这篇文章传送门本来是记录自己练手的demo的,后来很多人来问我要代码.今天就抽时间封装了一下,没有考虑太多情况,等我有空再去仔细考虑吧. 代码在:Github 用法很简单,创建对象,设置数据源,记 ...