2017-9-17pat甲级 C
来源:互联网 发布:淘宝托管真的有用吗 编辑:程序博客网 时间:2024/05/18 00:28
C. 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 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 2Sample Output:
NoYesYesNoNo
题目大意:给n个结点m条边,再给k个集合。对这k个集合逐个进行判断。每个集合S里面的数字都是结点编号,
求问整个图所有的m条边两端的结点,是否至少一个结点出自集合S中。如果是,输出Yes否则输出No
分析:用vector v[n]保存某结点属于的某条边的编号,比如a b两个结点构成的这条边的编号为0,
则v[a].push_back(0),v[b].push_back(0)——表示a属于0号边,b也属于0号边。对于每一个集合做判断,
遍历集合中的每一个元素,将当前元素能够属于的边的编号i对应的hash[i]标记为1,表示这条边是满足有一个结点出自集合S中的。
最后判断hash数组中的每一个值是否都是1,如果有不是1的,说明这条边的两端结点没有一个出自集合S中,则输出No。否则输出Yes~
代码:
#include <iostream>#include <vector>using namespace std;int main() { int n, m, k, nv, a, b, num; scanf("%d%d", &n, &m); vector<int> v[n]; for (int i = 0;i < m; i++) { scanf("%d%d", &a, &b); v[a].push_back(i); v[b].push_back(i); } scanf("%d", &k); for (int i = 0; i < k; i++) { scanf("%d", &nv); int flag = 0; vector<int> hash(m, 0); for (int j = 0; j < nv; j++) { scanf("%d", &num); for (int t = 0; t < v[num].size(); t++) hash[v[num][t]] = 1; } for (int j = 0; j < m; j++) { if (hash[j] == 0) { printf("No\n"); flag = 1; break; } } if (flag == 0) printf("Yes\n"); } return 0;}
- 2017-9-17pat甲级 C
- 2017-9-17pat甲级 A
- 2017-9-17pat甲级 B
- 2017年9月17日PAT甲级考试反思
- 【PAT甲级】【C++】1003. Emergency (25)
- 【PAT甲级】【C++】1004. Counting Leaves (30)
- 【PAT甲级】【C++】1008. Elevator (20)
- PAT甲级C语言.1004. 成绩排名
- PAT甲级C语言.1009. 说反话
- PAT 甲级
- PAT甲级.1065. A+B and C (64bit) (20)
- 1065. A+B and C (64bit) (20) PAT 甲级
- 【PAT甲级】【C】1001. A+B Format (20)
- 【PAT甲级】【C】1002. A+B for Polynomials (25)
- 【PAT甲级】【C++】1006. Sign In and Sign Out (25)
- 【PAT甲级】【C++】1005. Spell It Right (20)
- 【PAT甲级】【C++】1007. Maximum Subsequence Sum (25)
- 【PAT甲级】【C++】1009. Product of Polynomials (25)
- Http请求中post和get的区别
- Fragment+Fragment上的自动无限轮播+XListView
- 使用Glide获取图片缓存大小以及清除缓存
- java_SSM框架浅析
- BZOJ 4517 [Sdoi2016]排列计数 数论
- 2017-9-17pat甲级 C
- 22. Generate Parentheses(生成括号)
- Okhttp封装,结合ImageLoader,
- 当前时间
- 使用Runtime类 识别 sdcard和U盘android 6.0 存储路径识别
- 【数据结构】最短路径——Dijkstra算法
- stata12.13免费破解版网址
- mysql在查询结果列表前添加一列递增的序号列(最简)
- 本地缓存