求助,能过案例,但是洛谷MLE和WA
查看原帖
求助,能过案例,但是洛谷MLE和WA
967480
jiunichenyin楼主2024/4/4 19:45
#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define maxn 100005
#define mod 23333
#define base 233
using namespace std;
int n, m;  
vector<int> tu[maxn];
int point[maxn];
//inline int read() {
//    int w = 1, q = 0;
//    char ch = ' ';
//    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
//    if (ch == '-') w = -1, ch = getchar();
//    while (ch >= '0' && ch <= '9') q = q * 10 + ch - '0', ch = getchar();
//    return w * q;
//}
queue<int> q;
void dfs(int x) {
	if (point[x] == 0) {
		cout <<x << " ";
		point[x] = 1;
	}
	for (int i = 0; i <tu[x].size(); ++i) {
			dfs(tu[x][i]);
	}
}
int main() {
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int x, y;
		cin >> x >> y;
		tu[x].push_back(y);
	}
	dfs(1);
	cout << endl;
	memset(point, 0, sizeof(point));
	int x = 1;
	q.push(x);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		cout << now << " ";
		for (int i = 0; i <tu[now].size(); ++i) {
			if (point[tu[now][i]] == 0) {
				q.push(tu[now][i]);
				point[tu[now][i]] = 1;
			}
		}
	}
	return 0;
}
2024/4/4 19:45
加载中...