提交记录 6938


用户 题目 状态 得分 用时 内存 语言 代码长度
Simpson561 2002. 【NOIP2018】旅行(加强版) Accepted 100 238.019 ms 88936 KB C++11 2.88 KB
提交时间 评测时间
2018-11-17 13:29:44 2020-08-01 00:54:32
#include <stdio.h>
#include <algorithm>

namespace IO {
	const int BUF_SIZE = 1 << 24;
	char in_buf[BUF_SIZE], out_buf[BUF_SIZE];
	char *p_in_buf = in_buf + BUF_SIZE;
	char *p_out_buf = out_buf;
	
	
	inline char get_char() {
		if (p_in_buf == in_buf + BUF_SIZE) {
			fread(in_buf, 1, BUF_SIZE, stdin), p_in_buf = in_buf;
		}
		return *(p_in_buf++);
	}
	
	inline void put_char(char x) {
		if (p_out_buf == out_buf + BUF_SIZE) {
			fwrite(out_buf, 1, BUF_SIZE, stdout), p_out_buf = out_buf;
		}
		*(p_out_buf++) = x;
	}
	
	inline void flush() {
		if (p_out_buf != out_buf) {
			fwrite(out_buf, 1, p_out_buf - out_buf, stdout);
			p_out_buf = out_buf;
		}
	}
}

#define getchar() IO::get_char()
#define putchar(x) IO::put_char(x)

inline int getint() {
	int x = 0;
	char c = getchar();
	while (c <= 32) c = getchar();
	int f = 1;
	if (c == '-') f = -1, c = getchar();
	while (c > 32) x = x * 10 + c - 48, c = getchar();
	return x * f;
}

template <class T>
inline void _putint(T x) {
	return x ? _putint(x / 10), putchar(48 + x % 10), void() : void();
}

template <class T>
inline void putint(T x) {
	if (x < 0) return putchar('-'), putint(-x), void();
	return x == 0 ? putchar('0'), void() : _putint(x), void();
}

const int MAXN = 500005;

int n, m;

struct edge {
	int u, v, id;
};

bool operator < (const edge &a, const edge &b) {
	return a.u < b.u || (a.u == b.u && a.v < b.v);
}

edge edges[MAXN * 2];
edge *first[MAXN];

int stack[MAXN];
int stack_eid[MAXN];
bool instack[MAXN];
int top;
bool found;

bool is_on_cycle[MAXN];

void dfs(int u, int last) {
	stack[++top] = u;
	instack[u] = true;
	for (edge *e = first[u]; e->u == u; e++) {
		int v = e->v;
		if (v == last) continue;
		if (instack[v]) {
			found = true;
			is_on_cycle[e->id] = true;
			while (stack[top] != v) {
				is_on_cycle[stack_eid[top - 1]] = true;
				--top;
			}
			return;
		}
		stack_eid[top] = e->id;
		dfs(v, u);
		if (found) return;
	}
	instack[u] = false;
	--top;
}

int ans_idx;

bool visit[MAXN];
bool del;

void dfs(int u) {
	putint(u), putchar(" \n"[++ans_idx == n]);
	visit[u] = true;
	++top;
	bool done = false;
	for (edge *e = first[u]; e->u == u; e++) {
		int v = e->v;
		if (visit[v]) continue;
		edge *e1 = e + 1;
		while (e1->u == u && visit[e1->v]) ++e1;
		if (e1->u == u) {
			stack[top] = e1->v;
		} else {
			--top;
			done = true;
		}
		if (!del && is_on_cycle[e->id] && v > stack[top]) {
			del = true;
		} else {
			dfs(v);
		}
	}
	if (!done) --top;
}

int main() {
	n = getint(), m = getint();
	if (n == 1) {
		return putchar('1'), putchar('\n'), IO::flush(), 0;
	}
	for (int i = 0; i < m; i++) {
		int u, v;
		u = getint(), v = getint();
		edges[i * 2] = (edge){u, v, i};
		edges[i * 2 + 1] = (edge){v, u, i};
	}
	std::sort(edges, edges + 2 * m);
	first[1] = edges;
	for (int i = 1; i < 2 * m; i++) {
		if (edges[i].u != edges[i - 1].u) {
			first[edges[i].u] = edges + i;
		}
	}
	dfs(1, 0);
	top = 0;
	stack[0] = n + 1;
	dfs(1);
	IO::flush();
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #19.34 us48 KBAcceptedScore: 5

Testcase #210.7 us48 KBAcceptedScore: 5

Testcase #324.38 us60 KBAcceptedScore: 5

Testcase #423.74 us60 KBAcceptedScore: 5

Testcase #5894.8 us628 KBAcceptedScore: 5

Testcase #6888.14 us596 KBAcceptedScore: 5

Testcase #725.462 ms11 MB + 120 KBAcceptedScore: 5

Testcase #825.348 ms9 MB + 96 KBAcceptedScore: 5

Testcase #9233.112 ms57 MB + 536 KBAcceptedScore: 5

Testcase #10236.964 ms62 MB + 824 KBAcceptedScore: 5

Testcase #11235.424 ms47 MB + 480 KBAcceptedScore: 5

Testcase #12238.019 ms53 MB + 176 KBAcceptedScore: 5

Testcase #13936.85 us652 KBAcceptedScore: 5

Testcase #14908.24 us760 KBAcceptedScore: 5

Testcase #1524.713 ms12 MB + 368 KBAcceptedScore: 5

Testcase #1624.934 ms13 MB + 536 KBAcceptedScore: 5

Testcase #17231.74 ms86 MB + 872 KBAcceptedScore: 5

Testcase #18231.612 ms86 MB + 872 KBAcceptedScore: 5

Testcase #19214.123 ms59 MB + 452 KBAcceptedScore: 5

Testcase #20226.252 ms74 MB + 748 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-22 18:48:58 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠