pat甲级 1134 Vertex Cover (25分)

本文深入解析PAT甲级1134VertexCover题目,介绍顶点覆盖概念,提出创新的边编号存储策略,避免使用邻接矩阵和邻接表,通过实例代码演示高效解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pat甲级 1134 Vertex Cover (25分)

1.分析

note:只需要明白两点,(1)题目第一句话:
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. 什么意思。
(2)能否想到给边编号存储,不用邻接矩阵不用邻接表

这道题会就满分不会就零分。

2.代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<bool> visit;
vector<vector<int>> edge;
int main()
{
    int n,m,k,a,b,nv;
    cin>>n>>m;
    edge.resize(n);
    visit.resize(m);
    for(int i=0;i<m;++i)
    {
        cin>>a>>b;
        edge[a].push_back(i);
        edge[b].push_back(i);
    }
    cin>>k;
    while(k--)
    {
        cin>>nv;
        fill(visit.begin(),visit.end(),true);
        bool flag=true;
        while(nv--)
        {
            cin>>a;
            for(auto i:edge[a]) visit[i]=false;
        }
        for(auto i:visit) if(i) {flag=false;break;}
        printf("%s\n",flag?"Yes":"No");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值