1134. Vertex Cover (25)
1134. Vertex Cover (25)
时间限制 600 ms内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
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
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的1134. Vertex Cover (25)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Log4Net 配置
- 下一篇: 栈及其应用