3月8日 概念专题
A - 边覆盖
Problem Description
对一个给定的无向图G(V,E),边集E’是E的子集。如果V中的所有顶点都在E’中出现过,那么称边集E’是图G的一个边覆盖(Edge Cover)。
(以上定义引自https://en.wikipedia.org/wiki/Edge_cover)
根据上面的定义,请判断一些给定的边集是否是给定的无向图的边覆盖。
Input
每个输入文件一组数据。
第一行为两个整数N、M(1<=N<=500, 1<=M<=N*(N-1)/2),分别表示无向图的顶点数和边数。假设图上的顶点编号为从1到N。
接下来M行,每行两个正整数u、v(1<=u,v<=N, u!=v),分别表示一条无向边的两个端点。数据保证没有重边。
接着一个正整数K(K<=10),表示查询的个数。
然后是K个查询,每个查询第一行为一个正整数L(L<=M),表示欲查询边集E’中的边数;接下来L行,每行两个整数,表示边集E’中的一条边。数据保证E’一定是E的子集。
Output
每个查询一行,如果欲查询边集E’不是图G的边覆盖,那么输出No
;否则输出Yes
。
Sample Input
6 7
1 2
1 3
2 3
2 4
3 5
4 5
4 6
3
3
1 2
3 5
4 6
4
1 2
2 3
4 5
4 6
3
1 2
2 3
4 6
Sample Output
Yes
Yes
No
分析:先判断E‘是否是E’的子集,再判断E’有没有覆盖全部顶点
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<unordered_map>
#include<set>
#include<queue>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<limits>
using namespace std;
const int nmax = 510;
struct node{
int u, v;
};
unordered_map<int, bool>mp;
bool vis[nmax] = {false};
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, m, K, L;
cin>>n>>m;
for(int i = 0; i < m; ++i){
int u, v;
cin>>u>>v;
mp[u * nmax + v] = mp[v * nmax + u] = true;
}
cin>>K;
for(int i = 0; i < K; ++i){
fill(vis, vis + nmax, false);
bool flag = true;
cin>>L;
for(int j = 0; j < L; ++j){
int u, v;
cin>>u>>v;
vis[u] = vis[v] = true;
if(mp[u * nmax + v] == false)flag = false;
}
if(flag == true){
for(int j = 1; j <= n; ++j){
if(vis[j] == false){
flag = false;
break;
}
}
}
printf("%s\n", flag == true ? "Yes" : "No");
}
return 0;
}
B - 极大独立集
Problem Description
对一个给定的无向图G(V,E),点集V’是V的子集。如果V’中的任意两个顶点之间都没有边,就称点集V’是图G的独立集(Independent Set)。在此基础上,如果往V’中添加任何一个在V中但不在V’中的顶点,都会使V’变成非独立集,那么就称V’是图G的极大独立集(Maximal Independent Set)。
(以上定义引自https://en