1134. Vertex Cover (25)

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 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;
		else
			cout << "No" << endl;
	}

	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章