05-图1. List Components (25)

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

时间: 2024-07-30 10:11:34

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

数据结构学习笔记05图 (邻接矩阵 邻接表--&gt;BFS DFS)

数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边<v, w> 表示从v指向w的边(单行线) 不考虑重边和自回路 无向图:边是无向边(v, w) 有向图:边是有向边<v, w> 连通:如果从V到W存在一条(无向)路径,则称V和W是连通的 连通图(Connected Graph):如果对于图的任一两个顶点v.w∈V,v和w都是连通的,则称

List Components (25)

神器气爽!就是简单的深搜和广搜啦! #include <iostream> #include <queue> using namespace std; bool map[10][10]; bool dfsed[10]; int n, e; queue<int> Q; bool bfsed[10]; void bfs(int x); void dfs(int x); int main() { cin >> n >> e; for (int i =

06-图1. List Components (25) (邻接表实现)

繁难的二逼邻接表法  为了不拉低智商请勿模仿 #include <stdio.h> #include <malloc.h> #include <queue> using namespace std; const int MAXSIZE = 1000; typedef int ElementType; struct ListNode { ElementType Data; ListNode *Next; }; ListNode* CreateList(int data =

jquery easyui+sparkline插件+jqplot插件实现数据表行内插入线形图

Jquery easyui : 实现前端数据包格式化输出,支持多种模块式定义,只需要添加相应的预设参数即可实现丰富的前端. 资料参考: http://www.jeasyui.com/ Jquery sparkline: 基于jQuery 的js插件,底层调用html5的canvas标签,并通过js动态实现在数据表行内画条形图或趋势图,操作简单,可实现不同类型的图形化,如线形图,饼状图,柱形图. 资料参考:http://omnipotent.net/jquery.sparkline/#s-docs

使用JQuery制作幻灯片(轮播图)

1.首先看一下目录结构 images文件夹放所需要播放的图片. js文件夹放jquery库和main.js 2.html代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>JQuery slideShow</title> 6 <style> 7 *{margin: 0;

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

最近做的项目中需要用饼状图显示——'问卷调查'的统计结果(之前用过FusionChart做过柱状图的数据展示,那还是两年前的事了),在网上查了下FusionChart实现饼状图显示方面的资料,却发现资料都比较零散.不完整,或者说没有简洁明了的说清楚其用法,到FusionChart官网上去看,无奈是英文的,而且Deom项目太大,没有耐心去看,最后还是将以前的项目找出来,摸索着弄了下搞定,现将FusionChart实现柱状图.饼状图的动态数据显示的方法和经验写出来,希望对做web开发需要的朋友能有所

股票行情K线图Android版

转载请注明出住:http://blog.csdn.net/andywuchuanlong 现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有K线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下. 下面是做出来的效果图: 这个 界面 是如何画出来的我就不做介绍了,大家可以去下载项目源码. 背景图是利用canvas先画出一个矩形,然后再画几根虚线,均线图是通过path来绘制的,总之图的绘制是很简单的,我就不在这里作介绍了,大家可以去gith

jq龙禧轮播图

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style type="text/css"> 7 *{ 8 padding:0; 9 margin:0; 10 } 11 .carousel{ 12 height:6

Python绘制语谱图+时域波形

1 """Python绘制语谱图""" 2 """Python绘制时域波形""" 3 4 # 导入相应的包 5 import numpy, wave 6 import matplotlib.pyplot as plt 7 import numpy as np 8 import os 9 10 filepath = 'G:/实战培训/Python生成语谱图/ReNoise/Prim10/'