浙大数据结构: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;
}
相关推荐
hxung21 分钟前
B+树与红黑树
数据结构·b树
龙俊杰的读书笔记1 小时前
[leetcode] 面试经典 150 题——篇9:二叉树(番外:二叉树的遍历方式)
数据结构·算法·leetcode·面试
sml259(劳改版)3 小时前
数据结构--堆
数据结构·算法·
独家回忆3645 小时前
每日算法-250409
算法
青椒大仙KI115 小时前
25/4/6 算法笔记<仿真O2DES>基础知识学习
笔记·学习·算法
井云智能AI矩阵系统5 小时前
数字人情感表达突破:微表情自动生成的算法革新
算法
一只鱼^_7 小时前
第十五届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组
c++·蓝桥杯·深度优先·迭代加深·图搜索算法
一只码代码的章鱼7 小时前
数据结构与算法-图论-复习2(差分约束,强连通分量,二分图,LCA,拓扑排序,欧拉路径和欧拉回路)
数据结构·算法·图论
梁辰兴8 小时前
数据结构实验3.3:求解迷宫路径问题
数据结构·算法·深度优先·数组
triticale8 小时前
【数据结构】树状数组
数据结构