vector邻接表建图+DFS+BFS

本文介绍了使用vector构建邻接表来表示图,并详细讲解了如何进行深度优先搜索(DFS)和广度优先搜索(BFS)。内容侧重于ACM竞赛中的图论应用。
摘要由CSDN通过智能技术生成

以边操作为主的图用边集数组存储比较好,相比链式前向星,vector建图更容易懂。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
using LL = long long;
const int inf = 0x3f3f3f3f;
const int MAX_E = 1e5+5; // 最大边数
const int MAX_V = 1e4+5; // 最大节点数
int V, E; // 点、边
struct edge{ int to, w; };
vector<edge> G[MAX_E];
bool vis[MAX_V];

void input();
void Build(int x, int y, int w);
void _dfs(int s);
void dfs(int s); // 封装了memset
void bfs(int s);

int main()
{
    input();
    dfs(0);
    cout << endl;
    bfs(0);
    return 0;
}

void Build(int x, int y, int w = 1)
{
    edge e; e.to = y; e.w = w;
    G[x].push_back(e);
}

void input()
{
    int a,b,w;
    edge e;
    cin >> V >> E;
    for (int i = 0; i < E; i++)
    {
        cin >> a >> b >> w; // 带权
        //cin >> a >> b; // 不带权
        Build(a, b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值