社区讨论

RE 了求调

P5318【深基18.例3】查找文献参与者 4已保存回复 7

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lpl4hq3d
此快照首次捕获于
2023/11/30 19:38
2 年前
此快照最后确认于
2023/11/30 21:25
2 年前
查看原帖
为什么全 RE 呢?
我在 DEV-C++ 上没有输入输出就 return 了个 $3321225477$

CPP
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e6 + 5;
int gr[N][N];
int used[N];
int n, m;

void dfs(int x) {
	if (used[x])
		return;
	cout << x << ' ';
	used[x] = 1;
	for (int i = 1; i <= n; i++)
		if (gr[x][i] == 1 && !used[i])
			dfs(i);
}

void bfs(int x) {
	queue<int> q;
	q.push(x);
	used[x] = 1;
	while (!q.empty()) {
		int h = q.front();
		q.pop();
		cout << h << " ";
		for (int i = 1; i <= n; i++) {
			int t = gr[h][i];
			if (!used[i]) {
				q.push(i);
				used[i] = 1;
			}
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int a, b;
		cin >> a >> b;
		gr[a][b] = 1;
	}
	dfs(1);
	cout << endl;
	for (int i = 1; i <= N; i++)
		used[i] = 0;
	bfs(1);
	return 0;
}

回复

7 条回复,欢迎继续交流。

正在加载回复...