社区讨论
求助92pts #17 #22WA
P5049[NOIP 2018 提高组] 旅行 加强版参与者 4已保存回复 6
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 6 条
- 当前快照
- 1 份
- 快照标识符
- @lobvcit1
- 此快照首次捕获于
- 2023/10/30 03:33 2 年前
- 此快照最后确认于
- 2024/11/25 16:43 去年
10以下小数据三个小时没拍出来(而且不是用时间做随机种子,就是每次种子都不一样那种)
CPP#include<bits/stdc++.h>
#define int long long
using namespace std;
#define getchar() (read.getc())
#define putchar(c) (print.putc(c))
#define puts(s) MYPUTS(s)
struct in {
char buf[1 << 20], * p1, * p2;
inline char getc() { return (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); }
int operator()() {
int x = 0, f = 1; char ch = getc();
while (!isdigit(ch) && ch != EOF) { if (ch == '-')f = -1; ch = getc(); }
while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getc(); }
return x * f;
}
}read;
struct out {
char buf[1 << 20], * p, * end;
out() { p = buf; end = buf + (1 << 20); }
inline void putc(char c) { if (p == end)fwrite(buf, 1, 1 << 20, stdout), p = buf; *p++ = c; }
void operator()(int a) {
if (a < 0)putc('-'), a = -a;
if (a >= 10)(*this)(a / 10);
putc(a % 10 ^ 48);
}
~out() { fwrite(buf, 1, p - buf, stdout); }
}print;
inline void MYPUTS(const char* str) { for (int i = 0; str[i]; i++)putchar(str[i]); putchar('\n'); }
int n = read(), m = read();
vector<int>e[500010];
int sta[500010], idx;
bool vis[500010], h[500010], ed;
void tarjan(int d) {
sta[++idx] = d, vis[d] = 1;
for (vector<int>::iterator it = e[d].begin(); it != e[d].end(); it++) {
if (sta[idx - 1] != *it) {
if (vis[*it] == 1) {
while (idx && sta[idx] != *it)h[sta[idx--]] = 1;
h[*it] = ed = 1;
}
else tarjan(*it);
}
if (ed)break;
}
idx--, vis[d] = 0;
}
int t;
void dfs(int d, int up, int front) {
if (t ^ d)print(d), putchar(' ');
if (!h[d])
for (auto&& i : e[d]) {
if (vis[i])continue;
vis[i] = 1, dfs(i, 0, 0);
}
else {
int Max = *e[d].rbegin(), st=front, sec=up;
for (auto&& i : e[d]) {
if (vis[i])continue;
if (i == Max)break;
if (h[i]) { st = d, sec = *(&i + 1); break; }
}
for (auto&& i : e[d]) {
if (vis[i])continue;
if (h[i] && i > up && up && i==Max)
t = front, memset(h, 0, sizeof(h)), dfs(front, 0, 0);
else vis[i] = 1, dfs(i, sec, st);
}
}
}
signed main() {
for (int i = 1; i <= m; i++) {
int a = read(), b = read();
e[a].emplace_back(b);
e[b].emplace_back(a);
}
for (int i = 1; i <= n; i++)
sort(e[i].begin(), e[i].end());
tarjan(1), idx = 0;
vis[1] = 1, dfs(1, 0, 0);
return~EOF;
}
提前感谢!
回复
共 6 条回复,欢迎继续交流。
正在加载回复...