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. 最小生成树Prim算法 Kruskal算法

    Prim算法(贪心策略)N^2 选定图中任意定点v0,从v0开始生成最小生成树 树中节点Va,树外节点Vb 最开始选一个点为Va,其余Vb, 之后不断加Vb到Va最短距离的点 1.初始化d[v0]=0 ...

  2. eclipse修改xml文件默认的打开方式为XML Editor

    1.菜单:Window   ->   Preferences   ->   General   ->   Editors   ->   File   Associations  ...

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

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

  4. vue -vantUI tab切换时 list组件不触发load事件解决办法

    最近由于公司项目需要,用vue写了几个简单的页面.用到了vantUI List 列表 瀑布流滚动加载,用于控制长列表的展示 当列表即将滚动到底部时,会触发事件并加载更多列表项. (页面加载完成后默认会 ...

  5. 51nod 1057 n的阶乘 (压位优化)

    题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057&judgeId=605203 使用压位进行优化, ...

  6. 笔试算法题(47):简介 - B树 & B+树 & B*树

    B树(B-Tree) 1970年由R. Bayer和E. Mccreight提出的一种适用于外查找的树,一种由BST推广到多叉查找的平衡查找树,由于磁盘的操作速度远小于存储器的读写速度,所以要求在尽量 ...

  7. oracle的备份方式

    一.完全备份 exp 用户/密码@库名 file=存储位置 二.RMAN https://www.cnblogs.com/Latiny/p/6920428.html RMAN在数据库服务器的帮助下实现 ...

  8. c++基础_字符串对比

    #include <iostream> #include <string.h> #include <algorithm> using namespace std; ...

  9. hdu 2377 Bus Pass

    Bus Pass Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. Windows系列原版系统镜像下载

    原版系统镜像下载 Windows 10 系统 Windows 10 企业版 1511版 (64位) Windows 10 Enterprise, Version 1511 (x64) – DVD (C ...