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