05-图1. List Components (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered from 0 to N-1. While searching, assume that we always start from the vertex with the smallest index,
and visit its adjacent vertices in ascending order of their indices.

Input Specification:

Each input file contains one test case. For each case, the first line gives two integers N (0<N<=10) and E, which are the number of vertices and the number of edges, respectively. Then E lines follow, each described an edge by giving the two ends. All the numbers
in a line are separated by a space.

Output Specification:

For each test case, print in each line a connected component in the format "{ v1 v2 ... vk }". First print the result obtained by DFS, then by BFS.

Sample Input:

8 6
0 7
0 1
2 0
4 1
2 4
3 5

Sample Output:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }

#include <stdio.h>
void DFS(int graph[][10], int *visited, int v, int n, int *ret) {
visited[v] = 1;
ret[++ret[0]] = v;
for (int i = 0; i < n; ++i) {
if (v != i && graph[v][i] && !visited[i]) {
DFS(graph, visited, i, n, ret);
}
}
}
void BFS(int graph[][10], int *visited, int v, int n) {
int queque[20] = {};
int head = 0, rear = 0;
queque[rear++] = v; //入队
visited[v] = 1;
printf("{ ");
while (rear > head) {
int curr = queque[head++]; //出队
printf("%d ", curr);
for (int i = 0; i < n; ++i) //将每一个每訪问过的邻接节点入队
if (graph[curr][i] && !visited[i]) {
visited[i] = 1;
queque[rear++] = i;
}
}
printf("}\n");
}
int main() {
// freopen("test.txt", "r", stdin);
int n, edge;
scanf("%d%d", &n, &edge);
int graph[10][10] = {};
for (int i = 0; i < edge; ++i) { //邻接矩阵方式构造图
int v1, v2;
scanf("%d%d", &v1, &v2);
graph[v1][v2] = 1;
graph[v2][v1] = 1;
}
int visited[10] = {};
for (int i = 0; i < n; ++i) {
int ret[11] = {}; //保存递归遍历到的节点
if (!visited[i]) {
DFS(graph, visited, i, n, ret);
printf("{ ");
for (int j = 1; j <= ret[0]; ++j) {
printf("%d ", ret[j]);
}
printf("}\n");
}
}
for (int i = 0; i < n; ++i) //重置已訪问标记
visited[i] = 0;
for (int i = 0; i < n; ++i) { //BFS
if (!visited[i]) {
BFS(graph, visited, i, n);
}
} return 0;
}

题目链接:http://www.patest.cn/contests/mooc-ds/05-%E5%9B%BE1

05-图1. List Components (25)的更多相关文章

  1. pat05-图1. List Components (25)

    05-图1. List Components (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue For a ...

  2. 数据结构学习笔记05图(最小生成树 Prim Kruskal)

    最小生成树Minimum Spanning Tree 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 树: 无回路   |V|个顶 ...

  3. 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)

    数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...

  4. 【(图) 旅游规划 (25 分)】【Dijkstra算法】

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...

  5. FusionChart实现柱状图、饼状图的动态数据显示 附Demo

    最近做的项目中需要用饼状图显示——'问卷调查'的统计结果(之前用过FusionChart做过柱状图的数据展示,那还是两年前的事了),在网上查了下FusionChart实现饼状图显示方面的资料,却发现资 ...

  6. android 股票K线图

    现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有K线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下. 下面是做出来的效果图: 这个 界面 ...

  7. Github项目推荐-图神经网络(GNN)相关资源大列表

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 转自 | AI研习社 作者|Zonghan Wu 这是一个与图神经网络相关的资源集合.相关资源浏览下方 ...

  8. scrum立会报告+燃尽图(第二周第二次)

    此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2247 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...

  9. FusionChart实现柱状图、饼状图的动态数据显示

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. Vue之x-template(1)

    今天,我们来讲一个比较有趣的一个功能吧 先来看一段代码示例: <html> <head> <meta charset="utf-8"> < ...

  2. expdp dblink

    客户端创建dblik create public database link [link_name] connect to {username} identified by "{passwo ...

  3. NET VBCSCompiler.exe占用100%,造成项目卡顿的的解决方法

    1)服务器环境 最低配 的window server 2008 r2, 配置低容易发现问题‘ 2)事件描述 :项目打开缓慢,查询列表卡顿 3)问题分析:排除代码问题, ->打开服务器任务管理器 ...

  4. NET使用SuperSocket完成TCP/IP通信

    1)为什么使用SuperSocket? 性能高,易上手.有中文文档,我们可以有更多的时间用在业务逻辑上,SuperSocket有效的利用自己的协议解决粘包 2)SuperSocket的协议内容? 命令 ...

  5. swiper 3D 覆盖流的使用方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  6. 禁止浏览器static files缓存篇

    由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. Meta法 目前在chrome调试还没有遇到问题,好用!此方法假设浏览器是个好人!很听我们的话! <meta http ...

  7. 零基础入门学习Python(9)--了不起的分支和循环3

    前言 本节继续介绍分支和循环 知识点 while循环 Python while循环与if条件分支有点类似,在条件为真的情况下,执行某一段指定的代码.不同的是只要条件为True,while循环就会一直重 ...

  8. (九)python3 列表生成式

    列表生成式即 List Comprehensions,是 Python 内置的非常简单却强大的可以用来创建 list 的生成式. 要生成 list [1, 2, 3, 4, 5, 6, 7, 8, 9 ...

  9. js高级程序设计第八章BOM(未完成,待续)

    8.1window对象 BOM的核心对象是window,表示浏览器的一个实例. window对象有双重角色,既可以通过就是访问浏览器窗口的接口,又是ECMAscript规定的Global对象   8. ...

  10. 【Codeforces 329B】Biridian Forest

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 找到出口到每个点的最短距离. 设你到出口的最短距离为temp 那么如果某个人到终点的距离<=temp,则他们肯定能遇到你 因为他们可以在 ...