1134. Vertex Cover (25)
1134. Vertex Cover (25)
時間限制 600 ms內(nèi)存限制 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個節(jié)點的邊,給遇到的邊上標記。最后如果有沒標記的就是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 ,所以做的時候總是從點出發(fā),然后思緒就很混亂。看了別人的代碼以后才發(fā)現(xiàn)只要數(shù)邊就行了 _(:з)∠)_
思路歷程:
先想到的是用鄰接矩陣存儲,遍歷Nv個節(jié)點所在行的值,把連通的地方(1)改成0,最后遍歷全圖,如果還存在邊(1) 則輸出No,反之Yes 。接著發(fā)現(xiàn)是10^4,想想鐵定超時。
進而改用鄰接表存儲,遍歷Nv個節(jié)點的邊的同時刪除邊,最后如果還存在邊,則為No,反之Yes。寫的時候發(fā)現(xiàn):
1:有k組數(shù)據(jù)要測試,直接刪會影響后面處理
2:刪除對應頂點的邊時,得要搜索,時間復雜度提高
之后就走了邪路,想著用點亮頂點的方法? 結果第一組數(shù)據(jù)就是頂點全亮但少一條邊的情況
看題解才知道點亮邊就行了……
PS:搜的時候看到另外一種思路,保存所有邊的信息,對于每一條邊的兩個頂點,在Vn頂點的集合中查找。如果兩個頂點都不在集合中,說明這條邊沒有被覆蓋
?博客網(wǎng)址: 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
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的1134. Vertex Cover (25)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Log4Net 配置
- 下一篇: 栈及其应用