浙大数据结构: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;
}
相关推荐
Y1nhl1 小时前
力扣_二叉树的BFS_python版本
python·算法·leetcode·职场和发展·宽度优先
syzyc2 小时前
[ABC267F] Exactly K Steps
数据结构·动态规划·题解
向阳逐梦2 小时前
PID控制算法理论学习基础——单级PID控制
人工智能·算法
2zcode2 小时前
基于Matlab多特征融合的可视化指纹识别系统
人工智能·算法·matlab
Owen_Q3 小时前
Leetcode百题斩-二分搜索
算法·leetcode·职场和发展
矢志航天的阿洪3 小时前
蒙特卡洛树搜索方法实践
算法
草莓熊Lotso3 小时前
【数据结构初阶】--顺序表(二)
c语言·数据结构·经验分享·其他
汤姆爱耗儿药3 小时前
数据结构——散列表
数据结构·散列表
UnderTheTime4 小时前
2025 XYD Summer Camp 7.10 筛法
算法
zstar-_4 小时前
Claude code在Windows上的配置流程
笔记·算法·leetcode