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 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 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:
Nv v[1] v[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
#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 分)——图遍历的更多相关文章
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- 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 ...
- 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 ...
- 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 ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- 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 ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
随机推荐
- 汇编语言--CPU资源和存储器(二)
二.CPU资源和存储器 需要访问的硬件资源主要有:CPU内部资源.存储器和I/O端口. 1.寄存器组 (1)16位寄存器组 16位CPU所含有的寄存器有(见图2.1中16位寄存器部分): 4个数据寄存 ...
- 数组去重(ES5、ES6)
对象赋值思想:(接收后台数据绘制统计图用到了该想法) // ES5 /** * 数组.字符串去重 * @param {[string/array]} [数组] * @return {[array]} ...
- JAVA 和.NET在安全功能的比较
以下转载于: http://www.it28.cn/ASPNET/825159.html 本文根据Denis Piliptchouk的文章翻译.摘录而来,有些术语翻译不太好理解,还请参考原文. 第一部 ...
- Linux 学习笔记之超详细基础linux命令 Part 14
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 11
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...
- NoHttp封装--03 cookie
NoHttp请求自动维持Cookie: 1.支持Session.Cookie.临时Cookie的位置. 2.支持App重启.关机开机后继续持久化维持. 3.提供了接口,允许开发者监听Coo ...
- sql语句进阶教程
转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...
- WFE和WFI的区别
1. 概念: WFI(Wait for interrupt)和WFE(Wait for event)是两个让ARM核进入low-power standby模式的指令,由ARM architecture ...
- 將UNITY作品上傳到Facebook App!
前言 大家好,今天要來介紹如何用UNITY 將製作好的遊戲上傳到Facebook,也就是Facebook App.近期Facebook與Unity合作而推出了新的插件,利用插件可上傳分數.邀請好友.P ...
- ccf-20161203--权限查询
这题我的思路是将用户直接与他的权限联系起来.比如: 用户 角色 权限 Alice hr crm:2直接转变为:Alice: crm:2 题目与代码如下: 问题描述 试题编号: 201612-3 试题名 ...