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. apt-get update 报错 W: Unknown Multi-Arch type 'no' for package 'compiz-core'

    源 #deb包 deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http:// ...

  2. NSFileHandle类

    Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作 NSFileHandle类中得方法可以对文件进行基本的读写,偏移量的操作.NSFileHandle基本步骤:1. ...

  3. 关于latch: cache buffers chains的sql优化

    前段时间,优化了一些耗buffer比较多的sql,但是CPU使用率还是没下来 . 查看操作系统CPU使用率 查看awr,发现又有一条超级耗性能的sql冒出来了. 该SQL每次执行耗费3e多个buffe ...

  4. js 技巧 (三)

    //无模式的提示框 function modelessAlert(Msg) {    window.showModelessDialog("javascript:alert("&q ...

  5. HTML5地理定位-Geolocation API

    HTML5提供了一组Geolocation API,来自navigator定位对象的子对象,获取用户的地理位置信息Geolocation API使用方法:1.判断是否支持 navigator.geol ...

  6. 将json格式转为url参数格式的方法(xjl456852整理修改)

    测试页面: <html> <head> <script type="text/javascript" src="jquery-1.11.3. ...

  7. Android : reletive layout

    在兄弟的上下左右: android:layout_toRightOf="@id/btn1"/> android:layout_toLeftOf="@id/img1& ...

  8. navicat mysql 连接本地 忘记密码 查看密码 操作

    https://jingyan.baidu.com/article/454316ab4e9e65f7a7c03ad1.html

  9. requests模块发送POST请求

    在HTTP协议中,post提交的数据必须放在消息主体中,但是协议中并没有规定必须使用什么编码方式,从而导致了 提交方式 的不同.服务端根据请求头中的 Content-Type 字段来获知请求中的消息主 ...

  10. 【PowerDesigner】PowerDesigner之CDM、PDM、SQL之间转换

    有关CDM.PDM.SQL之间转换以及不同数据库之间库表Sql的移植,首先要了解的是它们各自的用途.这里就简单的描述一下,不做详细的解释了. CDM:概念数据模型.CDM就是以其自身方式来描述E-R图 ...