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

#头文件路径,多个路径行填充,前一个路径后必须用” \”结束,最后一个路径不带” \” INCDIR = -I../../extern_lib/common_inc\ -I../../extern_lib/cpu_inc\ -I../../extern_lib/cpu_cfg\ -I../../egrp_ble_multi_5515/ble_mbtc/inc\ -I../../egrp_ble_multi_5515/ble_sdk/inc\ -I../../egrp_ble_multi_5515/config -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\drivers_ext\gr55xx -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\drivers_ext\st7735 -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\fault_trace -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\fcc -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\gui -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\hal_flash -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\hci_uart -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\pmu_calibration -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\ring_buffer -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\sensorsim -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\user_efuse -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\utility -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\virt_key -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_alarm -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_assert -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_error -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_key -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_linked_list -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_log -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_memory -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_queue -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_scheduler -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\app_timer -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\at_cmd -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\ble -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\CMSIS -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\crypto_lib -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\dfu_master -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\libraries\dfu_port -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\components\sdk -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\drivers\inc -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\external\segger_rtt -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\platform\arch\arm\cortex-m\cmsis\core\include -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\platform\boards -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\platform\include -I../../egrp_src\middleware\GR551x_SDK_V2.0.2\platform\soc\include 把\按照规则加上
最新发布
06-13
在你的代码中,`\` 用于将多行内容连接为一行。但是,从你的代码片段来看,有些路径缺少了必要的 `\` 来进行换行连接。以下是修正后的代码: ```makefile INCDIR = -I../../extern_lib/common_inc\ -I../../extern_lib/cpu_inc\ -I../../extern_lib/cpu_cfg\ -I../../egrp_ble_multi_5515/ble_mbtc/inc\ -I../../egrp_ble_multi_5515/ble_sdk/inc\ -I../../egrp_ble_multi_5515/config\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/drivers_ext/gr55xx\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/drivers_ext/st7735\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/fault_trace\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/fcc\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/gui\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/hal_flash\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/hci_uart\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/pmu_calibration\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/ring_buffer\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/sensorsim\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/user_efuse\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/utility\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/virt_key\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_alarm\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_assert\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_error\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_key\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_linked_list\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_log\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_memory\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_queue\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_scheduler\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/app_timer\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/at_cmd\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/ble\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/CMSIS\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/crypto_lib\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/dfu_master\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/libraries/dfu_port\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/components/sdk\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/drivers/inc\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/external/segger_rtt\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/platform/arch/arm/cortex-m/cmsis/core/include\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/platform/boards\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/platform/include\ -I../../egrp_src/middleware/GR551x_SDK_V2.0.2/platform/soc/include ``` ### 解释 1. **`\` 的作用**:在 Makefile 中,`\` 用于将多行内容连接成一行。如果某一行的末尾没有 `\`,那么编译器会认为这是独立的一行,可能导致错误。 2. **修正点**: - 在 `-I../../egrp_ble_multi_5515/config` 后添加了 `\`。 - 将所有的反斜杠路径隔符统一为 `/`(如果你的系统是 Windows,也可以保留为 `\`,但需要确保转义正确)。 - 确保最后一行不带 `\`。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值