17年春季第三题 PAT甲级 1134 Vertex Cover (25分)

本文介绍了一道PAT甲级编程题,题目要求判断给定的多个顶点集是否为图的顶点覆盖。通过使用vector和结构体存储图的边,并遍历每条边检查其端点是否至少有一个在查询的顶点集中,来实现顶点覆盖的判断。

题目来源:https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088

备考汇总贴:2020年6月PAT甲级满分必备刷题技巧

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 10^4​​), 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​​ v[1] v[2]⋯v[N​v​​]

where N​v​​ 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

题目大意

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

题目分析

用vector和结构体存边,然后遍历每条边,如果存在一条边,它的两个顶点都不在查询的集合里,那就输出No,否则输出Yes。

满分代码

#include<iostream>
#include<vector>
using namespace std;
struct edge{
    int a,b;
};
int main(){
    int n,m,k,e,x;
    scanf("%d %d",&n,&m);
    vector<edge> g(m);
    for(int i=0;i<m;i++){
        scanf("%d %d",&g[i].a,&g[i].b);
    }
    cin>>k;
    while(k--){
        cin>>e;
        set<int> s;
        for(int j=0;j<e;j++){
            scanf("%d",&x);
            s.insert(x);
        }
        bool f=true;
        for(int i=0;i<m;i++){
            if(s.find(g[i].a)==s.end()&&s.find(g[i].b)==s.end()){
                cout<<"No"<<endl;
                f=false;
                break;
            }
        }
        if(f)cout<<"Yes"<<endl;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

### 反馈顶点集问的定义 反馈顶点集 (Feedback Vertex Set, FVS)图论中的一个重要概念,在计算机科学领域具有广泛应用。给定一个无向图 \( G=(V,E) \),FVS 定义为最小数量的节点集合 \( S\subseteq V \),使得移除这些节点后的子图不含任何环路[^1]。 形式化描述如下: - 输入:无向图 \( G(V,E) \) - 输出:节点集合 \( S \subset V \),满足条件: - 对于任意简单回路 \( C \in G \),存在至少一个节点 \( v_i \in C \cap S \) 这意味着当删除了所有的反馈顶点之后,剩下的图是一个森林结构,即由若干棵不相交树组成。 ### 解决方案概述 针对这一 NP 完全问,目前有多种算法可以近似解决或精确求解特定规模下的实例。主要为两大类方法: #### 近似算法 对于大规模实际应用而言,通常采用多项式时间复杂度的近似算法来获得接近最优解的结果。一种常见的策略是贪心法,通过迭代选取局部最优解逐步构建全局解决方案。例如,可以选择每次从未处理过的最大度数节点开始,直到无法再找到新的循环为止[^2]。 另一种有效的方法基于线性规划松弛技术,利用整数线性规划模型表示原问,并通过对偶理论得到下界估计值;随后借助随机舍入或其他启发式手段转换成可行解[^3]。 ```python def greedy_fvs(graph): fvs_set = set() while has_cycle(graph): node_to_remove = max_degree_node(graph) remove_node_and_adjacent_edges(node_to_remove, graph) fvs_set.add(node_to_remove) return list(fvs_set) ``` #### 参数化算法 参数化计算提供了一种更精细的方式来衡量问难度,特别是对于那些难以在一般情况下高效求解的问。对于固定参数可追踪(Fixed Parameter Tractable, FPT)性质的研究表明,如果限定反馈顶点集中元素数目 k,则可以在 O*(f(k)) 时间内完成搜索过程,其中函数 f() 的增长速度远低于指数级别。 具体实现上,可以通过支限界法不断缩小候选范围直至收敛到确切答案。这种方法虽然理论上能够保证找到最佳解,但在实践中往往受限于输入尺寸和可用资源约束。 ### 应用场景举例 反馈顶点集问广泛应用于多个领域: - **网络设计**:在网络拓扑优化过程中,识别并消除冗余连接有助于简化架构、降低成本; - **生物信息学**:蛋白质相互作用网析中去除自催化反应路径有利于揭示核心调控机制; - **数据库查询计划生成**:减少不必要的表扫描操作提高SQL执行效率等。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值