浙大数据结构:06-图1 列出连通集

这道题就是建图并熟悉一下dfs和bfs,比较简单

1、条件准备

存图准备,用邻接矩阵,visit看该结点是否走过
cpp 复制代码
#include <iostream>
#include <string.h>
using namespace std;

int n, m;//结点数,边数
int graph[20][20];//邻接矩阵存
int visit[15];//是否访问该节点
主函数先输入结点数和边数,然后存图,无向图存两次。
然后dfs遍历输出每一个连通集,然后visit置0,再bfs每一个连通集,注意输出格式。
cpp 复制代码
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++)
  {
    int a, b;
    cin >> a >> b;
    graph[a][b] = 1;graph[b][a] = 1;
  }

  for (int i = 0; i < n; i++)
  {
    if (!visit[i])
    {
      cout << "{ ";
      dfs(i);
      cout << "}";
      cout << endl;
    }
  }
  memset(visit, 0, sizeof(visit));

  for (int i = 0; i < n; i++)
  {
    if (!visit[i])
    {
      cout << "{ ";
      bfs(i);
      cout << "}";
      cout << endl;
    }
  }
  return 0;
}

2、dfs遍历

dfs使用递归实现,访问到该节点就输出,visit置1.
然后遍历所有结点,如果没走过并且有路就走。
cpp 复制代码
void dfs(int x)
{
  cout << x << ' ';
  visit[x] = 1;
  for (int i = 0; i < n; i++)
    if (!visit[i] && graph[x][i])
      dfs(i);
}

3、bfs遍历

用数组模拟队列来进行bfs,先把头存入队列并输出。
然后遍历队列直到为空,每一次循环先取队头,再遍历所有结点。
若没走过且有路则输出并放入队列中。
cpp 复制代码
void bfs(int head)
{
  int q[20], tt = -1, hh = 0;
  q[++tt] = head;
  visit[head] = 1;
  cout << head << ' ';
  while (hh <= tt)
  {
    int cur = q[hh++];
    for (int i = 0; i < n; i++)
    {
      if (!visit[i] && graph[cur][i])
      {
        cout << i << " ";
        visit[i] = 1;
        q[++tt] = i;
      }
    }
  }
}

4、总结

这题较为简单,难度不大。
完整代码如下:
cpp 复制代码
#include <iostream>
#include <string.h>
using namespace std;

int n, m;
int graph[20][20];
int visit[15];

void dfs(int x)
{
  cout << x << ' ';
  visit[x] = 1;
  for (int i = 0; i < n; i++)
    if (!visit[i] && graph[x][i])
      dfs(i);
}

void bfs(int head)
{
  int q[20], tt = -1, hh = 0;
  q[++tt] = head;
  visit[head] = 1;
  cout << head << ' ';
  while (hh <= tt)
  {
    int cur = q[hh++];
    for (int i = 0; i < n; i++)
    {
      if (!visit[i] && graph[cur][i])
      {
        cout << i << " ";
        visit[i] = 1;
        q[++tt] = i;
      }
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++)
  {
    int a, b;
    cin >> a >> b;
    graph[a][b] = 1;graph[b][a] = 1;
  }

  for (int i = 0; i < n; i++)
  {
    if (!visit[i])
    {
      cout << "{ ";
      dfs(i);
      cout << "}";
      cout << endl;
    }
  }
  memset(visit, 0, sizeof(visit));

  for (int i = 0; i < n; i++)
  {
    if (!visit[i])
    {
      cout << "{ ";
      bfs(i);
      cout << "}";
      cout << endl;
    }
  }
  return 0;
}
相关推荐
电院工程师6 分钟前
SM3算法Python实现(无第三方库)
开发语言·python·算法·安全·密码学
小刘同学++43 分钟前
用 OpenSSL 库实现 3DES(三重DES)加密
c++·算法·ssl
写写闲篇儿2 小时前
搜索二维矩阵
线性代数·算法·矩阵
LunaGeeking2 小时前
重要的城市(图论 最短路)
c++·算法·编程·图论·最短路·floyd
刘小小_算法工程师2 小时前
「ECG信号处理——(17)基于小波熵阈值的R峰检测(与时域-频域-多尺度小波法对比)」2025年6月12日
算法·信号处理
电控极客2 小时前
电动汽车驱动模式扭矩控制设计方法
经验分享·算法·汽车·策略模式
lyh13442 小时前
在macOS上运行Linux容器的方法
数据结构·状态模式
jz_ddk2 小时前
[python] 使用python设计滤波器
开发语言·python·学习·算法
1白天的黑夜13 小时前
二叉树-226.翻转链表-力扣(LeetCode)
数据结构·c++·leetcode
快乐肚皮3 小时前
快速排序优化技巧详解:提升性能的关键策略
java·算法·性能优化·排序算法