博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT-1134. Vertex Cover (25)
阅读量:4677 次
发布时间:2019-06-09

本文共 4301 字,大约阅读时间需要 14 分钟。

1134. Vertex Cover (25)

时间限制
600 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 118 76 84 58 48 11 21 49 89 11 02 454 0 3 8 46 6 1 7 5 4 93 1 8 42 2 87 9 8 7 6 5 4 2
Sample Output:
NoYesYesNoNo

 

这道题的关键是容易超时,自己写的代码仔细想想确实会超时,再参考了别人的代码,感觉妙哉。将每条边编号,记录每个顶点相关的边,将询问的顶点相关的边都标记了,再遍历所有的边,发现没有标记的说明没有全覆盖。

也可以使用set容器,将所有询问顶点放入set中,遍历所有的边,判断set中是否有边的两个任意一个顶点,若没有则没有全覆盖。

 

#include 
using namespace std;int N, M, K;struct Node { int x; int y;};vector
nodVec, nodVec1;void deleteNode(int x, vector
nodV) {}int main(){ cin>> N>> M; int x, y; for(int i = 0; i < M; i++) {
//10^4 scanf("%d%d", &x, &y); Node node; node.x = x; node.y = y; nodVec.push_back(node); } cin>> K; nodVec1 = nodVec; for(int i = 0; i < K; i++) {
//10^2 scanf("%d", &x); for(int j = 0; j < x; j++) {
//10^4 scanf("%d", &y); vector
::iterator it = nodVec1.begin(); while(it != nodVec1.end()) {
//10^4每次都会遍历所有的边,虽然有erase,但是总量还是很大。 if(y == it->x || y == it->y) { it = nodVec1.erase(it); } else { it++; } } } if(nodVec1.empty()) { printf("Yes\n"); } else { printf("No\n"); } nodVec1 = nodVec; } return 0;}

 

 

#include
using namespace std;#define MAX 10004int N, M, K;vector
nodes[MAX];int flag[MAX];int main() { cin>> N>> M; int x, y; for(int i = 0; i < M; i++) {
//10^4 scanf("%d%d", &x, &y); nodes[x].push_back(i); nodes[y].push_back(i); } cin>> K; for(int i = 0; i < K; i++) {
//10^2 for(int j = 0; j < M; j++) {
//10^4 flag[j] = 0; } scanf("%d", &x); for(int j = 0; j < x; j++) {
//10^4*2;这里每次遍历一个顶点的所有边,总共最多遍历两次所有的边 scanf("%d", &y); vector
::iterator it; for(it = nodes[y].begin(); it != nodes[y].end(); it++) { flag[*it] = 1; } } int flag2 = 1; for(int j = 0; j < M; j++) { if(flag[j] == 0) { flag2 = 0; printf("No\n"); break; } } if(flag2 == 1) { printf("Yes\n"); } } return 0;}

 

#include
using namespace std;int N, M, K;#define MAX 10004struct Node { int x, y;}nodes[MAX];set
nodeSet;int main() { cin>>N>>M; for(int i = 0; i < M; i++) {
//10^4 scanf("%d%d", &nodes[i].x, &nodes[i].y); } cin>>K; int x, y; for(int i = 0; i < K; i++) {
//10^2 cin>> x; nodeSet.clear(); for(int j = 0; j < x; j++) {
//10^4 scanf("%d", &y); nodeSet.insert(y); } int flag = 1; for(int i = 0; i < M; i++) {
//10^4*log10^4 if(nodeSet.find(nodes[i].x) == nodeSet.end() && nodeSet.count(nodes[i].y) == 0) { flag = 0; break; } } if(!flag) { printf("No\n"); } else { printf("Yes\n"); } } return 0;}

 

转载于:https://www.cnblogs.com/ACMessi/p/8495610.html

你可能感兴趣的文章
mysqlin会使用索引吗_被面试官虐了,索引为何使用B+树,你知道吗
查看>>
mysql8单节点500m_Kubernetes 部署 Mysql 8.0 数据库(单节点)
查看>>
mysql数据工厂生产_MySQL超时与天蓝色数据工厂副本
查看>>
python缩进可以用在任何语句之后_每天一道Python选择题--python缩进
查看>>
mysql查询左边大于左边_MySQL WHERE 子句
查看>>
java 获取颜色_java关于照片属性的获取,颜色模式
查看>>
session丢失原因 java_session没有过期,其保存的数据无故丢失的原因
查看>>
java pkcs 11 write_java pkcs#11读取证书加解密(初学-分享)
查看>>
tranisant java_java tranisant
查看>>
java ibatis 存储过程_ibatis 调用存储过程
查看>>
java中的softreference_Java语言中内存优化的SoftReference 和 WeakReference的对比分析
查看>>
java提供了丰富的类库_Java优势有哪些?
查看>>
java 过滤器权限控制_JAVA过滤器,实现登陆权限限制
查看>>
设计模式java 模板模式_java设计模式--模板方法模式
查看>>
中缀转后缀 java_Java 利用堆栈将中缀表达式转换成后缀
查看>>
java执行sql解析_java执行SQL语句实现查询的通用方法详解
查看>>
java中keepalived开启方式_高可用之KeepAlived(一):基本概念和配置文件分析
查看>>
java中的ejb_JAVA语言中关于EJB技术概论
查看>>
java有date类型吗_关于java中date类型的问题
查看>>
java中svg图片怎么用_svg如何使用
查看>>