【题解】【AcWing】1627. 顶点覆盖

153 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1627. 顶点覆盖

原题传送:AcWing 1627. 顶点覆盖

如果图中的一个顶点集合能够满足图中的每一条边都至少有一个端点在该集合内,那么这个顶点集合就是图的顶点覆盖。

现在给定一张图,以及若干个顶点集合,请你判断这些顶点集合是否是图的顶点覆盖。

输入格式

第一行包含两个整数 NNMM ,表示图中点和边的数量。

接下来 MM 行,每行包含两个整数 a,ba,b ,表示点 aa 和点 bb 之间存在一条边。

点编号 0N10 \sim N-1

然后包含一个整数 KK ,表示询问的顶点集合数量。

接下来 KK 行,每行描述一组询问顶点集合,格式如下:

NvN_v V[1]V[1] V[2]V[2]V[Nv]V[N_v]

NvN_v 是集合中点的数量, V[i]V[i] 是点的编号。

输出格式

每组询问输出一行结果,如果是顶点覆盖则输出 Yes,否则输出 No

数据范围

1N,M1041 \le N,M \le 10^4 , 1K1001 \le K \le 100 , 1NvN1 \le N_v \le N

输入样例:

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

输出样例:

No
Yes
Yes
No
No

思路:

记录边,遍历判断是否两个顶点有任意一个出现过,若均未出现,则未覆盖。

题解:

#include <bits/stdc++.h>

using namespace std;

const int N = 10010;

int n, m;
bool st[N];

struct Edge
{
	int a, b;
}e[N];

int main()
{
	cin >> n >> m;
		
	for(int i = 0; i < m; i++)
		cin >> e[i].a >> e[i].b;
	
	int k;
	cin >> k;
	while(k--)
	{
		int cnt;
		cin >> cnt;
		memset(st, 0, sizeof st);
		
		while(cnt--)
		{
			int x;
			cin >> x;
			st[x] = true;
		}
		
		int i;
		for(i = 0; i < m; i++)
			if(!st[e[i].a] && !st[e[i].b])
				break;
				
		if(i == m)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	
	return 0;
}