浙大数据结构: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;
}
相关推荐
亦梦亦醒乐逍遥22 分钟前
【C++基础】字符串/字符读取函数解析
java·c++·算法
捞鱼哲学家41 分钟前
【hot100】刷题记录(11)-搜索二维矩阵 II
数据结构·线性代数·算法·leetcode·矩阵
L_M_TY1 小时前
G1. Yunli‘s Subarray Queries (easy version)
算法·stl·滑动窗口·离线查询
查理零世2 小时前
【算法】回溯算法专题③ ——排列型回溯 python
python·算法·深度优先
利刃大大2 小时前
【数据结构与算法】九大排序算法实现详解
c语言·数据结构·c++·算法·排序算法
眼镜哥(with glasses)2 小时前
蓝桥杯python基础算法(2-1)——排序
数据结构·算法·蓝桥杯
Dovis(誓平步青云)2 小时前
线性数据结构:单向链表
数据结构·链表
qystca3 小时前
【16届蓝桥杯寒假刷题营】第2期DAY2
数据结构·c++·算法·深度优先·二分·爆搜
南宫生4 小时前
力扣动态规划-19【算法学习day.113】
java·学习·算法·leetcode·动态规划
和风化雨4 小时前
排序算法--插入排序
c语言·c++·算法·排序算法