1134. Vertex Cover (25)
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
题意:给出Nv个顶点,要求每条边都与顶点直接相连
思路:给每条边 编号,用邻接表存储 边的编号,遍历Nv个节点的边,给遇到的边上标记。最后如果有没标记的就是No,反之Yes。
代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;
vector <int> num[100000];
int main()
{int n, m, k;cin >> n >> m;int a, b;for (int i = 0; i < m; i++){cin >> a >> b;num[a].push_back(i); num[b].push_back(i);}cin >> k;while (k--){int judge = 1;cin >> a;vector <int> edge (m, 0);for (int i = 0; i < a; i++){cin >> b;for (int j = 0; j < num[b].size(); j++) edge[num[b][j]] = 1;}for (int i = 0; i < m; i++)if (!edge[i]) judge = 0;if (judge) cout << "Yes" << endl;else cout << "No" << endl;}
}
因为题目是 Vertex Cover ,所以做的时候总是从点出发,然后思绪就很混乱。看了别人的代码以后才发现只要数边就行了 _(:з)∠)_
思路历程:
先想到的是用邻接矩阵存储,遍历Nv个节点所在行的值,把连通的地方(1)改成0,最后遍历全图,如果还存在边(1) 则输出No,反之Yes 。接着发现是10^4,想想铁定超时。
进而改用邻接表存储,遍历Nv个节点的边的同时删除边,最后如果还存在边,则为No,反之Yes。写的时候发现:
1:有k组数据要测试,直接删会影响后面处理
2:删除对应顶点的边时,得要搜索,时间复杂度提高
之后就走了邪路,想着用点亮顶点的方法 结果第一组数据就是顶点全亮但少一条边的情况
看题解才知道点亮边就行了……
PS:搜的时候看到另外一种思路,保存所有边的信息,对于每一条边的两个顶点,在Vn顶点的集合中查找。如果两个顶点都不在集合中,说明这条边没有被覆盖
博客网址: http://blog.csdn.net/akibayashi/article/details/78014749
代码:
#include<iostream>
#include<set>
using namespace std;int main()
{int N, M, K, edges[10002][2];cin >> N >> M;for (int i = 0; i<M; i++){int a, b;cin >> a >> b;edges[i][0] = a;edges[i][1] = b;}cin >> K;for (int i = 0; i<K; i++){int n;bool flag = true;set<int> vset;cin >> n;for (int j = 0; j<n; j++){int input;cin >> input;vset.insert(input);}for (int j = 0; j<M; j++){if (vset.find(edges[j][0]) == vset.end() && vset.find(edges[j][1]) == vset.end()){flag = false;break;}}if (flag)cout << "Yes" << endl;elsecout << "No" << endl;}return 0;
}
转载于:https://www.cnblogs.com/childwang/p/7834752.html
1134. Vertex Cover (25)相关推荐
- PAT甲级1134 Vertex Cover :[C++题解]顶点覆盖、图论、用结构体存边,bool数组判断
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:首先把所有的边存储下来,使用结构体,结构体中保存两个端点. 然后对于每次询问,将出现过的顶点标志为true放在st数组中,然后遍历所有 ...
- 1134 Vertex Cover
题意:给出一个图和k个查询,每个查询给出Nv个结点,问与这些结点相关的边是否包含了整个图的所有边. 思路:首先,因为结点数较多,用邻接表存储图,并用unordered_map<int,unord ...
- 【PAT甲级】1134 Vertex Cover
✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
- 集合覆盖 顶点覆盖: set cover和vertex cover
这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...
- 二分图匹配 + 最小点覆盖 - Vertex Cover
Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点 ...
- 从顶点覆盖(Vertex Cover)到碰撞集(Hitting Set)的归约
碰撞集问题 给定一组集合{S1,S2,S3,-,Sn}和预算b,问是否存在一个集合H,其大小不超过b,且H和所有Si(i=1, 2, -, n)相交. 顶点覆盖问题 给定图G,问是否存在一个顶点集合V ...
- Vertex Cover问题
最近算法课上完成了一道作业题,Vertex Cover,拿出来与大家分享一下.算法不能说有多好,有问题欢迎致信ms08.shiroh@gmail.com 1,问题描述 首先Vertex Cover问题 ...
- scu 4439 Vertex Cover
题意: 给出n个点,m条边,将若干个点染色,使得每个边至少有一点染色,问至少染多少个点. 思路: 如果是二分图,那就是最小点覆盖,但是这是一般图. 一般图的最小覆盖是npc问题,但是这题有一个条件比较 ...
- 【题解】Luogu SP1435 PT07X - Vertex Cover
原题传送门 求树的最小点覆盖,就是一个树形dp 类似于没有上司的舞会 dp的状态为\(f[i][0/1]\),表示i节点是否选择 边界是\(f[x][0]=0\),\(f[x][1]=1\) 转移方程 ...
最新文章
- 六 Lync Server 2013 部署指南-OWA服务器部署
- 面向对象的三个基本特征 和 五种设计原则
- 写博客一定程度上是在刷存在感~
- Win64 驱动内核编程-17. MINIFILTER(文件保护)
- Learning ROS: Service and Client (C++)
- 频繁项目集java实现_关联分析(2):Apriori产生频繁项集
- Python中文转拼音代码(支持全拼和首字母缩写)
- jq.$post传递参数给php,通过URL参数post传递的实现方式 PHP/Javascript
- VS2010下载地址和安装教程(图解)
- Synology安装并实现在linux和win下使用ISCSI存储
- JS常见网页特效案例
- wd移动硬盘不能识别_wd移动硬盘读不出来怎么办
- java地铁售票机系统_Java_地铁购票系统
- Room的基本使用(一)
- Android轻松实现分享功能
- apt-get安装包失败提示The following packages have unmet dependencies
- reentrantlock与synch区别优点
- qt中文乱码原因分析及解决方案
- 算法设计与分析第二章作业
- IP公有地址和私有地址划分
热门文章
- 安装labelImg对数据集标注
- HitFilm Pro 12中文版
- Netty源码分析--NIO(一)
- Ubuntu16.04 使用sudo cat EOF 编辑文件,提示Permission denied错误的解决办法
- spark rdd Transformation和Action 剖析
- 中铁CA根证书安装不成功的解决办法
- JDBC与ORM发展与联系 JDBC简介(九)
- SSD行业要变天了!因为这种闪存芯片要来
- 【跃迁之路】【535天】程序员高效学习方法论探索系列(实验阶段292-2018.07.25)...
- D3DPOOL(资源池)