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. 20面向对象三特征 之继承 方法重写 super

    继承是:多个类有重复内容,把重复内容放到一个新类中,就可以通过extends关键词去让原来的类和新类产生继承关系,子类只能拿到父类一部分信息.通过extends关键词去指明类与类之间的关系,一个父类可 ...

  2. 聚类和EM算法——K均值聚类

    python大战机器学习——聚类和EM算法   注:本文中涉及到的公式一律省略(公式不好敲出来),若想了解公式的具体实现,请参考原著. 1.基本概念 (1)聚类的思想: 将数据集划分为若干个不想交的子 ...

  3. BZOJ 2039 人员雇佣 二元关系 最小割

    题面太长了,请各位自行品尝—>人员雇佣 分析: 借用题解的描述: a.选择每个人有一个代价Ai b.如果有两个人同时选择就可以获得收益Ei,j c.如果一个人选择另一个不选会产生代价Ei,j 这 ...

  4. [模板] Treap

    插入x 删除x 查询排名为x的数 查询x的排名 求x的前驱.后继 //Stay foolish,stay hungry,stay young,stay simple #include<iostr ...

  5. http2提升效率的几个点

    1.二进制传输,消息的解析效率更高 2.头部数据压缩,传输效率更高 3.多路复用,可以让请求并发执行 4.服务器推送,可以主动推送数据到浏览器 http2加载图片demo:https://http2. ...

  6. linux netstat-查看Linux中网络系统状态信息

    博主推荐:更多网络测试相关命令关注 网络测试  收藏linux命令大全 netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. 语法 netstat(选项) ...

  7. 21Spring重用切点表达式

    直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...

  8. C++——"%"运算符

    基本介绍 r=a%b,余数的取值范围为-b+1~b-1,符号与a一致 要想得到0~b-1的余数,可以用:(a%b+b)%b 数位拆解 方法一:反复的:%10,/10 sizea = 0; while ...

  9. javamail实现注册激活邮件

    http://www.jb51.net/article/111926.htm https://www.cnblogs.com/ganchuanpu/archive/2016/11/29/6115691 ...

  10. 贪心算法求解活动安排<算法分析>

    一.实验内容及要求 1.要求按贪心算法原理求解问题: 2.要求手工输入s[10]及f[10],其中注意自己判断s[i]<f[i]: 3.要求显示所有活动及最优活动安排的i事件列表.二.实验步骤  ...