acwing 847 图中点的层次

BFS求图中节点到1号节点距离

思路:因为各个弧的权都为1,所以用bfs ,先用 d[N] 数组保存每个点到 1 号节点的距离并且用 memset将所有节点距离设为-1表示未被遍历(正好省掉一个记录节点是否被遍历的数组的开销),等录入初始数据之后,开始从1 号节点进行整个图的遍历,遍历到的节点所对应位置保存该点到 1 号节点的距离,如果n号点未被遍历则返回-1,被遍历过则返回对应距离。

模拟队列

#include<iostream>
#include<cstring>
using namespace std;

const int N = 1e5 + 10;

int e[N],h[N],ne[N],idx;
int d[N],q[N];
int n,m;

void add(int a,int b)//建立邻接表,理同 846
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

int bfs()
{
    int hh = 0,tt = 0;//模拟栈,hh头,tt尾
    q[0] = 1;//设置起点
    
    memset(d ,-1, sizeof d);//-1 为没有遍历过
    
    d[1] = 0;//起点到起点的距离为 0
    while(hh<=tt)
    {
        int t = q[hh++];//出队&取首元素
        for(int i = h[t];i != -1;i = ne[i])
        {
            int j = e[i];
            if(d[j] == -1)//j 对应的点没有被遍历过
            {
                d[j] = d[t] + 1; //距离 + 1
                q[++tt] = j;//被遍历的点入队
            }
        }
    }
    return d[n];
}

int main()
{
    cin >> n >> m;
    memset(h,-1,sizeof h);
    for(int i = 0;i < m;i++)
    {
        int a,b;
        cin >> a >> b;
        add(a,b);
    }
    cout << bfs() << endl;
    return 0;
}

queue

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

const int N = 1e5 + 10;

int e[N],h[N],ne[N],idx;
int d[N];
int n,m;

void add(int a,int b)//建立邻接表,理同 846
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

int bfs()
{
    memset(d ,-1, sizeof d);//-1 为没有遍历过
    queue<int> q;
    d[1] = 0;//起点到起点的距离为 0
    q.push(1);
    
    while(!q.empty())
    {
        int t = q.front();//取首元素
        q.pop();//出队
        for(int i = h[t];i != -1;i = ne[i])
        {
            int j = e[i];
            if(d[j] == -1)//j 对应的点没有被遍历过
            {
                d[j] = d[t] + 1; //距离 + 1
                q.push(j);//被遍历的点入队
            }
        }
    }
    return d[n];
}

int main()
{
    cin >> n >> m;
    memset(h,-1,sizeof h);
    for(int i = 0;i < m;i++)
    {
        int a,b;
        cin >> a >> b;
        add(a,b);
    }
    cout << bfs() << endl;
    return 0;
}
### 连通块内节点计数算法 #### 广度优先搜索(BFS)实现连通块计数 广度优先搜索是一种适合用于遍历或搜索形结构的方法。对于连通块内的节点计数问题,可以采用BFS来解决。 给定一个无向G=(V,E),其中V代表顶点集合而E代表边集。为了统计每一个连通分量中的结点数目: - 使用队列辅助完成层次化探索过程; - 维护访问标记数组`vis[]`记录哪些位置已经被处理过; 当遇到未被访问过的节点u时启动新的BFS进程,并在此过程中累加当前联通区域大小直至无法继续扩展为止[^1]。 ```python from collections import deque def bfs_count(graph, start_node): queue = deque([start_node]) visited = set() count = 0 while queue: node = queue.popleft() if node not in visited: visited.add(node) count += 1 for neighbor in graph[node]: if neighbor not in visited: queue.append(neighbor) return count ``` 此方法能够有效地找出并计算出从指定起点可达的所有其他节点的数量,在面对大规模稀疏矩阵的情况下表现尤为出色[^2]。 #### 深度优先搜索(DFS)实现连通块计数 除了BFS之外,也可以利用递归形式的深度优先搜索来进行相同的操作。这种方法同样依赖于栈机制(隐式的函数调用堆栈),并且通过回溯的方式逐步深入到子树最底部再返回上级节点直到整个分支都被扫描完毕[^3]。 ```python def dfs_count(graph, current_node, visited=set()): if current_node in visited: return 0 visited.add(current_node) total = 1 for next_node in graph[current_node]: if next_node not in visited: total += dfs_count(graph, next_node, visited) return total ``` 这两种方式都可以很好地解决问题,具体选择取决于实际应用场景和个人偏好。通常来说,由于Python存在最大递归深度限制,默认情况下可能不适合非常深嵌套情况下的DFS操作[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值