提交记录 9130


用户 题目 状态 得分 用时 内存 语言 代码长度
Richard_for_OI 2002. 【NOIP2018】旅行(加强版) Accepted 100 355.344 ms 55568 KB C++ 1.68 KB
提交时间 评测时间
2019-04-13 21:18:05 2020-08-01 01:32:54
#include <stdio.h>
#include <algorithm>

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) {
	printf("%d%c", u, " \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() {
	scanf("%d%d", &n, &m);
	if (n == 1) {
		return puts("1"), 0;
	}
	for (int i = 0; i < m; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		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);
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #113.77 us44 KBAcceptedScore: 5

Testcase #212.92 us44 KBAcceptedScore: 5

Testcase #346.85 us52 KBAcceptedScore: 5

Testcase #445.19 us52 KBAcceptedScore: 5

Testcase #51.958 ms424 KBAcceptedScore: 5

Testcase #61.949 ms400 KBAcceptedScore: 5

Testcase #747.174 ms7 MB + 200 KBAcceptedScore: 5

Testcase #847.374 ms5 MB + 1000 KBAcceptedScore: 5

Testcase #9348.346 ms36 MB + 492 KBAcceptedScore: 5

Testcase #10352.916 ms39 MB + 664 KBAcceptedScore: 5

Testcase #11352.606 ms30 MB + 456 KBAcceptedScore: 5

Testcase #12355.344 ms33 MB + 892 KBAcceptedScore: 5

Testcase #131.995 ms436 KBAcceptedScore: 5

Testcase #141.972 ms500 KBAcceptedScore: 5

Testcase #1546.214 ms7 MB + 1000 KBAcceptedScore: 5

Testcase #1646.537 ms8 MB + 688 KBAcceptedScore: 5

Testcase #17347.498 ms54 MB + 272 KBAcceptedScore: 5

Testcase #18347.238 ms54 MB + 272 KBAcceptedScore: 5

Testcase #19329.11 ms37 MB + 844 KBAcceptedScore: 5

Testcase #20341.364 ms46 MB + 1020 KBAcceptedScore: 5


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