1134 Vertex Cover (25 分)

1134 Vertex Cover (25 分)

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 1), 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:

N?v?? [ [

where N?v?? is the number of vertices in the set, and [‘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

给n个结点m条边,再给k个集合。对这k个集合逐个进行判断。每个集合S里面的数字都是结点编号,求问整个图所有的m条边两端的结点,是否至少一个结点出自集合S中。如果是,输出Yes否则输出No

直接存是第几条边就行,然后用个set
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m,k,p;
 4 vector<int> v[10005];
 5 set<int> s;
 6 int main(){
 7     cin >> n >> m;
 8     int x,y;
 9     for(int i = 1; i <= m; i++){
10         cin >> x >> y;
11         v[x].push_back(i);
12         v[y].push_back(i);
13     }
14     cin >> k;
15     while(k--){
16         cin >> p;
17         set<int> st;
18         for(int i = 0; i < p; i++){
19             cin >> x;
20             for(int j = 0; j < v[x].size(); j++){
21                 st.insert(v[x][j]);
22             }
23         }
24         if(st.size() == m){
25             cout <<"Yes"<<endl;
26         }else{
27             cout <<"No"<<endl;
28         }
29     }
30     return 0;
31 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11300520.html

时间: 2024-07-29 17:22:07

1134 Vertex Cover (25 分)的相关文章

1134. Vertex Cover (25)

稍微做一点点手脚就不会超时了...还是挺简单的,考试时候也做出来了... #include<iostream> #include<string> #include<queue> #include<algorithm> using namespace std; const int inf = 99999999; int main() { int v, e; cin >> v >> e; int check[10001]; vector&

PAT 1134 Vertex Cover

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 Specif

PAT Advanced 1154 Vertex Coloring (25 分)

A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at most k colors is called a (proper) k-coloring. Now you are supposed to tell if a given

1142 Maximal Clique (25 分)图

1142 Maximal Clique (25 分) A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. A maximal clique is a clique that cannot be extended by including one more adjacent vertex. (Quoted f

1126 Eulerian Path (25 分)

1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard

1122 Hamiltonian Cycle (25 分)

1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle". In this problem, you are supposed to tell if a given cycle is a H

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy

二分图匹配 + 最小点覆盖 - Vertex Cover

Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点覆盖问题,二分图的最大匹配,直接套模版即可. Time complexity: O(N^2) view code

5-24 树种统计 (25分)

5-24 树种统计   (25分) 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(\le 10^5≤10?5??),随后N行,每行给出卫星观测到的一棵树的种类名称.种类名称由不超过30个英文字母和空格组成(大小写不区分). 输出格式: 按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位. 输入样例: 29 Red Alder Ash Aspe