60分,求调
查看原帖
60分,求调
939597
GreenDog01楼主2024/1/20 13:23

WA掉了#3和#4,找了好久也不到哪里错了,求大佬帮调一下

#include<bits/stdc++.h>
using namespace std;

const int N = 100005;
bool vis[N];
queue<int>q;

struct edge {
	int from;
	int to;
};
vector<edge>e[N];

bool cmp(edge x, edge y) {
	return x.to < y.to;
}

void DFS(int x) {
	vis[x] = true;
	cout << x << " ";
	for (int i = 0; i < e[x].size(); i++) {
		if (!vis[e[x][i].to])
			DFS(e[x][i].to);
	}
}

void BFS(int x) {
	q.push(x);
	vis[x] = true;
	cout << x << " ";
	while (!q.empty()) {
		int fro = q.front();
		for (int i = 0; i < e[fro].size(); i++) {
			if (!vis[e[fro][i].to]) {
				q.push(e[fro][i].to);
				cout << e[fro][i].to << " ";
				vis[e[fro][i].to] = true;
			}
		}
		q.pop();
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	memset(vis, false, sizeof(vis));
	int n, m, a, b;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		e[a].push_back({ a,b });
	}
	for (int i = 0; i < n; i++) {
		sort(e[i].begin(), e[i].end(), cmp);
	}
	DFS(1);
	memset(vis, false, sizeof(vis));
	cout << endl;
	BFS(1);
	return 0;
}
2024/1/20 13:23
加载中...