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 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容器)的更多相关文章
- 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甲级——1134 Vertex Cover (25 分)
1134 Vertex Cover (考察散列查找,比较水~) 我先在CSDN上发布的该文章,排版稍好https://blog.csdn.net/weixin_44385565/article/det ...
- 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 ...
- PAT 甲级 1134 Vertex Cover
https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...
- 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 ...
- 1134 Vertex Cover
题意:给出一个图和k个查询,每个查询给出Nv个结点,问与这些结点相关的边是否包含了整个图的所有边. 思路:首先,因为结点数较多,用邻接表存储图,并用unordered_map<int,unord ...
- PAT_A1134#Vertex Cover
Source: PAT A1134 Vertex Cover (25 分) Description: A vertex cover of a graph is a set of vertices su ...
- PAT1134:Vertex Cover
1134. Vertex Cover (25) 时间限制 600 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A vertex ...
随机推荐
- 【三剑客】awk命令2
1. 程序结构: Begin 和 End模块 awk的程序的结构:Begin块,Body块,End块. BEGIN块:BEGIN {awk-commands} BEGIN块在被程序启动时启动,且只执行 ...
- Vagrant (二) - 日常操作
立即上手 上一节中,我们介绍了怎样安装 Vagrant,安装本身并不困难.本章节中我们首先要快速上手,以便获得一个直观的概念: 建立一个工作目录 打开命令行工具,终端工具,或者iTerm2等,建立一个 ...
- Python 正则表达式——re模块介绍
Python 正则表达式 re 模块使 Python 语言拥有全部的正则表达式功能,re模块常用方法: re.match函数 re.match从字符串的起始位置匹配,如果起始位置匹配不成功,则matc ...
- dijkstra preiority_queue优化 紫书学习
#include<bits/stdc++.h> using namespace std; const int maxn=1000+10; const int INF=1000000000; ...
- SaltStack数据系统之Grains、Pillar
SaltStack数据系统之Grains.Pillar 1.什么是Grains? Grains是saltstack的组件,用于收集salt-minion在启动时候的信息,又称为静态信息.Grains是 ...
- 如何将MAC的 Terminal 行首变得清爽简洁一点?
作为一位开发人员,MAC带给我们更好的编程体验,Terminal也是经常会去操作的东西,但是说实话,默认的 Terminal 的各种设置,真的让我好难受 刚开始打开,可能看到的会是这样的,行首一大堆东 ...
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...
- 慎用ToLower和ToUpper,小心把你的系统给拖垮了
不知道何时开始,很多程序员喜欢用ToLower,ToUpper去实现忽略大小写模式的字符串相等性比较,有可能这个习惯是从别的语言引进的,大胆猜测下是JS,为了不引起争论,我指的JS是技师的意思~ 一: ...
- 【Hexo】使用Hexo+github pages+travis ci 实现自动化部署
目录 一.说明 二.成品展示 三.前期准备 本地安装 node.js 本地安装 git github 账号 创建仓库 travis ci 账号 四.安装 Hexo 五.使用 hexo 搭建博客 六.部 ...
- libevent(八)激活事件
激活事件添加流程 事件发生后,需要把对应的event加入到激活事件队列中. 整个流程如下: 对于定时器事件,在timeout_process过程中,会将事件从最小堆中删除. 激活事件处理流程 在eve ...