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

题目大意:n个顶点和m条边的图,分别给出m条边的两端顶点,然后对其进行k次查询,每次查询输入一个顶点集合,要求判断这个顶点集合是否能完成顶点覆盖,即图中的每一条边都至少有一个顶点在这个集合中。

主要思路:这道题最关键的就是图的建立,这里图的输入并不是给出的顶点及其邻接点的关系,而是给出所有边的两端顶点,如果仍然用二维矩阵的方法构造,后续的操作很容易就超时。这里用一个二维的vector容器,对于图中的每个点,添加其所有关联的边,用0 ~ m-1代表所有的m条边,图模型就构造好了。接着,对于点覆盖问题,可以转化成判断集合中每个点所关联的边加起来是否等于图的边数m,由于这里边不能重复,自然而然想到set容器,将要查询的集合中每个点的所有关联边加入set,如果数量等于图的边数m,则完成顶点覆盖,否则不能。

#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main(void) {
int n, m, i, j; cin >> n >> m;
vector<vector<int> > edge(n);
for (i = 0; i < m; i++) {
int v, w;
cin >> v >> w;
edge[v].push_back(i);
edge[w].push_back(i);
} int k, nv, ver, t;
set<int> s;
cin >> k;
for (i = 0; i < k; i++) {
cin >> nv;
for (j = 0; j < nv; j++) {
cin >> ver;
for (t = 0; t < edge[ver].size(); t++)
s.insert(edge[ver][t]);
}
if (s.size() == m) cout << "Yes" << endl;
else cout << "No" << endl;
s.clear(); //清空set集合
} return 0;
}

总结:构造模型时不要定式思维;关于查询,尽量用少的数据去多的数据里查,避免从多的数据往少的数据里查。

PAT-1134 Vertex Cover (图的建立 + set容器)的更多相关文章

  1. 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 ...

  2. PAT甲级——1134 Vertex Cover (25 分)

    1134 Vertex Cover (考察散列查找,比较水~) 我先在CSDN上发布的该文章,排版稍好https://blog.csdn.net/weixin_44385565/article/det ...

  3. 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 ...

  4. 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 ...

  5. PAT 甲级 1134 Vertex Cover

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...

  6. 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 le ...

  7. 1134 Vertex Cover

    题意:给出一个图和k个查询,每个查询给出Nv个结点,问与这些结点相关的边是否包含了整个图的所有边. 思路:首先,因为结点数较多,用邻接表存储图,并用unordered_map<int,unord ...

  8. PAT_A1134#Vertex Cover

    Source: PAT A1134 Vertex Cover (25 分) Description: A vertex cover of a graph is a set of vertices su ...

  9. PAT1134:Vertex Cover

    1134. Vertex Cover (25) 时间限制 600 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A vertex ...

随机推荐

  1. mpvue开发微信小程序之时间+日期选择器

    最近在做微信小程序,技术栈为mpvue+iview weapp组件库. 因项目需求,要用到日期+时间选择器,iview组件库目前还未提供时间日期选择器的组件,小程序官方组件日期时间也是分开的,在简书上 ...

  2. Elasticsearch: 权威指南 » 深入搜索 » 多字段搜索 » 多数字段 good

      跨字段实体搜索  » 多数字段编辑 全文搜索被称作是 召回率(Recall) 与 精确率(Precision) 的战场: 召回率 --返回所有的相关文档:精确率 --不返回无关文档.目的是在结果的 ...

  3. 自定义Bootstrap样式弹出框

    最近做的一些功能需要用到Bootstrap,然而原来的系统并没有引入Bootstrap,为了新写的控件能够应用于老的页面,又不需要在老的页面上引入全套的Bootstrap文件决定写一个模仿Bootst ...

  4. phpsocket.io

    https://github.com/walkor/phpsocket.io phpsocket.io A server side alternative implementation of sock ...

  5. 图论--2-SAT--POJ Ikki's Story IV - Panda's Trick

    Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping ...

  6. python-format方法记录

      今天写脚本,遇到了这种情况:需要上一个脚本的回参作为一个路径参数,我用的os.path.join()处理,因为这个路径参数在最后一位,但是没有考虑到如果路径参数在中间的话,这样的拼接就只能把后面的 ...

  7. python-函数变量与方法公有、私有整理

    关于函数与方法的区别: 函数: def funname(): 括号里面可以有形参,也可以没有,为空 在函数里面的参数整理: 方法: def funcname(self): 括号里面必须有个self,因 ...

  8. IOS抓取与反抓取

    目录 IOS抓取基础知识 IOS抓取方式 iOS破解 模拟器 黑雷苹果模拟器 介绍 局限 改机软件 常用改机软件 检测 可更改属性 注入与Hook(越狱下实现作弊) 注入方式 Hook方式 重打包(非 ...

  9. 在Vue中使用iview的Select控件实现一个多级选项列表

    前言 今天项目要实现一个多级选项列表,发现iview官网上没有写这个例子,于是自己就实现了,如果对你有帮助请点个赞 ‘ * ’!! 解决方法:下面我们就来使用V-for 来定义一个二级选项列表 ,代码 ...

  10. 《Docker从入门到跑路》之基本用法介绍

    Docker是一种轻量级的虚拟化技术,它具备传统虚拟机无法比拟的优势,它更简易的安装和使用方式.更快的速度.服务集成和开源流程自动化. Docker的安装 安装Docker的基本要素:1.Docker ...