社区讨论

萌新求助:DFS WA

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

讨论操作

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

当前回复
3 条
当前快照
1 份
快照标识符
@lodhcfro
此快照首次捕获于
2023/10/31 06:36
2 年前
此快照最后确认于
2023/11/06 21:50
2 年前
查看原帖
刚学搜索的彩笔求助 /kel
用的是 stl 里面的 stackqueue
知道可以改成递归。只是求助 stl 写法为什么会错()
CPP
# include <cctype>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <queue>
# include <stack>
# include <vector>
using namespace std;

typedef short unsigned int hu;
typedef long unsigned int lu;
enum { N = (const lu)1e5 };

const class IOstream {
public:
	inline const IOstream operator>>(lu&) const;
	inline const IOstream operator<<(lu) const;
	inline const IOstream operator<<(const char) const;
} io;

static lu n, m;
static vector<lu> o[N + 1];
static bool v[N + 1];

signed int main() {
	io >> n >> m;
	for (register lu i(0); i < m; ++i) {
		static lu x, y; io >> x >> y;
		o[x].push_back(y);
	}
	for (register lu i(1); i <= n; ++i) sort(o[i].begin(), o[i].end());
	{
		stack<lu> t; t.push(1), v[1] = true;
		while (not t.empty()) {
			const lu u(t.top()); t.pop(); io << u;
			for (lu i(o[u].size() - 1); i < o[u].size(); --i)
				if (not v[o[u][i]]) t.push(o[u][i]), v[o[u][i]] = true;
		}
		io << '\n', memset(v, 0, sizeof v);
	}
	{
		queue<lu> t; t.push(1), v[1] = true;
		while (not t.empty()) {
			const lu u(t.front()); t.pop(); io << u;
			for (lu i(0); i < o[u].size(); ++i) if (not v[o[u][i]]) t.push(o[u][i]), v[o[u][i]] = true;
		}
		io << '\n', memset(v, 0, sizeof v);
	}
	return 0;
}

inline const IOstream IOstream::operator>>(lu& a) const {
	char t; while (isspace(t = getchar())); a = 0; while (a = a * 10 + (t - '0'), isdigit(t = getchar()));
	return *this;
}
inline const IOstream IOstream::operator<<(lu a) const {
	char s[6]; hu l(0); while (s[l++] = a % 10 + '0', a /= 10); while (l) putchar(s[--l]); putchar(' ');
	return *this;
}
inline const IOstream IOstream::operator<<(const char a) const { putchar(a); return *this; }

回复

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

正在加载回复...