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 操作系统】Ubuntu 基础操作 基础命令 热键 man手册使用 关机 重启等命令使用
. : 关机, 如果将Linux默认运行等级设置为0, 系统将无法启动; -- : 多用户模式, 允许使用网络文件系统, 一般不使用图形界面登陆就是这种模式; -- : 多用户图形界面模式, 该模式下 ...
- 断言(Assertion)需要注意的一个地方
因为断言只在debug构建中有效,所以它是中关重要的去避免运行任何代码或调用任何方法在断言条件中,而这些代码或方法会影响程序的状态. 否则程序的行为将在debug和release构建中变得不一致,这显 ...
- 动态获取html页面的内容,并且取其中的某块元素的方法
$.ajax({ url: "http://192.168.1.59:8888/app-tpl-webapp/tpl/design.html", async:false, ...
- 【Matlab编程】生日快乐歌(显示歌词)—matlab版
clear all A4=440;%标准音A4 不同的曲调音调不同scale的取值范围不同 pt=44100;p0=pt/2;%频率 scale=A4/2^(9/12)*2.^((-12:11)/12 ...
- Git与远程reposiory的相关命令
问题1:Git如何同步远程repository的分支(branch) 某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢? 分析: ...
- Linux进程管理 - PRI,nice,free,uname,netstat
优先运行序 (priority, PRI) 这个 PRI 值越低代表越优先的意思.不过这个 PRI 值是由核心动态调整的, 使用者无法直接调整 PRI 值的. 由於 PRI 是核心动态调整的,我们使用 ...
- java时间操作
这篇讲的也很专业:http://soft.zdnet.com.cn/software_zone/2007/1129/660028.shtml java中的时间操作不外乎这四种情况: 1.获取当前时间 ...
- vs2010 matlab混合编程调用matlab引擎
// matlab_engine.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "engine.h" ...
- DB Query Analyzer 5.05 is released, 65 articles concerned have been published
DB Query Analyzer 5.05 is released, 65 articles concerned have been published DB Query Analyzer is p ...
- 恶补web之七:html DOM知识
html DOM定义了访问和操作html文档的标准;dom是w3c的标准,dom定义了访问html和xml文档的标准: w3c文档对象模型(dom)是中立平台和语言的接口,它允许程序和脚本动态访问和更 ...