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. 转载 基于JAVA每月运势api调用代码实例

    代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...

  2. 性能监控(6)–JAVA下的jinfo命令

    jinfo可以用来查看正在运行的java应用程序的扩展参数,设置支持在运行时,修改部分参数. Jinfo的语法为: Usage: jinfo [option] <pid> (to conn ...

  3. Gauss-Laguerre quadrature rule

    % matlab script to derive the 2-point Gauss-Laguerre quadrature rule % and use it on an example % in ...

  4. typedef struct LNode命名结构指针(线性表的链式存储)

    一.typedef 关键字 1. 简介: typedef工具是一个高级数据特性,利用typedef可以为某一些类型自定义名称. 2. 工作原理: 例如我们定义链表的存储结构时,需要定义结点的存储数据元 ...

  5. http请求之get和post的区别

    前言:大家现在度娘一下,查得最多的区别,可能就是: “Get把参数写在URL中,Post通过请求体来传参的” “GET没有POST安全,因为Get参数直接显示在URL上” “Get请求在URL中传送的 ...

  6. Stackoverflow 珠玑:用于分组的 LINQ 扩展方法

    从 stackoverflow.com 上抄来的,将 IEnumerable 中的元素进行切分的方法,无动态内存分配,地球上最快的实现: public static class LinqExtensi ...

  7. ComponetOne 2014 v3版本正式发布

    2014年11月18日---ComponentOne Studio Enterprise 2014 v3版全球正式发布.ComponentOne Studio Enterprise是世界知名的Micr ...

  8. 浅谈Java多线程同步机制之同步块(方法)——synchronized

    在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰的方法或者代码块,解决了资源共享.下面代码示意三个窗口购5张火车票: package com.jikexueyuan.t ...

  9. uCrop图片裁剪

    uCrop使用 github地址 https://github.com/Yalantis/uCrop然后clone或下载到本地,运行之. 效果预览 app/build.gradle compile ' ...

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

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