PAT1134:Vertex Cover
1134. 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
思路
判断一个集合的点是否覆盖了所有的边。
1.用vector保存所有的边(包含起止点)
2.将需要查询的点插入set容器中。
3.对于每一条边,检查它的起止点是否有至少一个点在集合中。如果有Yes,如果没有No。
代码
#include<iostream>
#include<vector>
#include<set>
using namespace std; class edge
{
public:
int a;
int b;
}; int main()
{
int N,M;
while(cin >> N >> M)
{
vector<edge> edges(M);
for(int i = 0; i < M; i++)
{
cin >> edges[i].a >>edges[i].b;
}
int K;
cin >> K;
for(int i = 0; i < K; i++)
{
int Nv;
cin >> Nv;
set<int> nodes;
for(int j = 0; j < Nv; j++)
{
int node;
cin >> node;
nodes.insert(node);
}
bool isCovered = true;
for(int v = 0; v < M; v++)
{
if(nodes.find(edges[v].a) == nodes.end() && nodes.find(edges[v].b) == nodes.end())
{
isCovered = false;
break;
}
}
if(isCovered)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
}
PAT1134:Vertex Cover的更多相关文章
- PAT-1134 Vertex Cover (图的建立 + set容器)
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...
- 集合覆盖 顶点覆盖: set cover和vertex cover
这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...
- URAL 2038 Minimum Vertex Cover
2038. Minimum Vertex Cover Time limit: 1.0 secondMemory limit: 64 MB A vertex cover of a graph is a ...
- 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 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 甲级 1134 Vertex Cover
https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...
- 二分图匹配 + 最小点覆盖 - Vertex Cover
Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点 ...
- SCU - 4439 Vertex Cover (图的最小点覆盖集)
Vertex Cover frog has a graph with \(n\) vertices \(v(1), v(2), \dots, v(n)\) and \(m\) edges \((v(a ...
- 四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)
Vertex Cover frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1), ...
随机推荐
- Linux下编译GDAL
一.准备工作 从官网下载GDAL.PROJ.4和GEOS,将其存放在/home/liml/Work/3rdPart目录并解压,如下图所示.下载地址请自行Google.注:使用的系统是CentOS6.4 ...
- Cocos2D v2.0至v3.x简洁转换指南(五)
资源管理 如果你没有计划用SpriteBuilder,你可以继续使用后缀去管理各种不同解决方案中的图像. 首先,你需要在AppDelegate.m中将[CCBReader configrueCCFil ...
- 一个小公式帮你轻松将IP地址从10进制转到2进制
网络工程师经常会遇到的一个职业问题:如何分配IP,通过子网如何捕捉某一网段或某台机器?他们甚至能够进行精准的分析和复杂的开发......凡此种种,其实与一些他们头脑中根深蒂固的常识性理论存有某种内在的 ...
- android数据保存之greendao
有时我们的数据属于保存到数据库,对于Android应用和IOS应用,我们一般都会使用SQLite这个嵌入式的数据库作为我们保存数据的工具.由于我们直接操作数据库比较麻烦,而且管理起来也非常的麻烦,以前 ...
- Ubuntu_ROS中应用kinect v2笔记
Ubuntu_ROS中应用kinect v2笔记 个人觉得最重要的资料如下: 1. Microsoft Kinect v2 Driver Released http://www.ros.org/new ...
- Make3D Convert your image into 3d model
Compiling and Running Make3D on your own computer source: http://make3d.cs.cornell.edu/code_linux.ht ...
- 页面缓存js问题解决
1.在jsp中加入头 <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP ...
- Linux - 简单好用的计算器 bc
如果在文本模式当中,突然想要作一些简单的加减乘除,偏偏手边又没有计算器!这个时候要笔算吗? Linux有提供一支计算程序,那就是bc.你在命令列输入bc后,屏幕会显示出版本信息, 之后就进入到等待指示 ...
- 安卓笔记-- ListView点击和长按监听
其中点击监听为setOnItemClickListener() 具体实现代码如下 listView.setOnItemClickListener(new AdapterView.OnItemClick ...
- How tomcat works 读书笔记十三 Host和Engine
Host Host是Context的父容器.如果想在一个tomcat上部署多个context就需要使用Host了.上下文容器的父容器是主机,但是可能有一些其它实现,没有必要的时候也可以忽略.不过在实践 ...