提交记录 6968


用户 题目 状态 得分 用时 内存 语言 代码长度
zjp_shadow 2002. 【NOIP2018】旅行(加强版) Accepted 100 542.037 ms 121700 KB C++11 2.74 KB
提交时间 评测时间
2018-11-25 08:48:56 2020-08-01 00:55:25
#include<bits/stdc++.h>

#define For(i, l, r) for(int i = (l), i##end = (int)(r); i <= i##end; ++ i)
#define Fordown(i, r, l) for(int i = (r), i##end = (int)(l); i >= i##end; -- i)
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
    int x(0), sgn(1); char ch(getchar());
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
    for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
    return x * sgn;
}

void File() {
#ifdef zjp_shadow
    freopen ("P5049.in", "r", stdin);
    freopen ("P5049.out", "w", stdout);
#endif
}

const int N = 5e5 + 1e3, M = N << 1, inf = 0x7f7f7f7f;

int n, m;

set<int> S[N];
int Head[N], Next[M], to[M], e = 0;

inline void add_edge(int u, int v) {
    to[++ e] = v; Next[e] = Head[u]; Head[u] = e;
	S[u].insert(v); S[v].insert(u);
}

bool vis[N], inc[N];
int ans[N], pre[N], len, pos;

inline void Paint(int u, int v) {
    pos = u;
    inc[u] = true;
    while (v != u) {
        inc[v] = true, v = pre[v];
    }
}

int dep[N];

void Dfs_Init(int u = 1, int fa = 0) {
    if (vis[u]) return ;
    dep[u] = dep[fa] + 1;
    vis[u] = true;
    for (int i = Head[u], v = to[i]; i; v = to[i = Next[i]]) 
        if (v != fa) {
            if (vis[v] && dep[u] < dep[v]) Paint(u, v);
            else pre[v] = u, Dfs_Init(v, u);
        }
}

inline int Get(int u) {
	while (!S[u].empty()) {
		auto v = S[u].begin();
		if (vis[*v]) S[u].erase(v);
		else return *v;
	}
    return inf;
}

int stk[N], sta[N], top;

void Dfs(int u) {
    if (!vis[u]) ans[++ len] = u;
    vis[u] = true;
    while(Get(u) < inf) Dfs(Get(u));
}

int minv[N], best = inf;

inline int Check() {
	Fordown(i, top - 1, 1)
		if (inc[stk[i]] && minv[stk[i]] != inf) return minv[stk[i]];
	return inf;
	//   return best;
}

bool back = true;

int main() {

	File();

	n = read(); m = read();
	For (i, 1, m) {
		int u = read(), v = read();
		add_edge(u, v);
		add_edge(v, u);
	}

	Dfs_Init(); 
	Set(vis, false); 

	stk[top = 1] = 1;
	while (top) {
		int u = stk[top];
		if (!vis[u]) ans[++ len] = u;
		vis[u] = true;
		minv[stk[top - 1]] = Get(stk[top - 1]);
		if (Get(u) == inf) { -- top; continue ; }
		if (!inc[u])
			Get(stk[++ top] = Get(u));
		else {
			bool flag = true;
			for (int i = Head[u], v = to[i]; i; v = to[i = Next[i]]) 
				if (!vis[v] && !inc[v]) flag = false;
			if (flag && back && Check() < Get(u)) {
				back = false;
				-- top;
				while (inc[stk[top]]) Dfs(stk[top --]);
			} else {
				stk[++ top] = Get(u);
			}
		}
	}

	For (i, 1, len)
		printf ("%d%c", ans[i], i == iend ? '\n' : ' ');

	return 0;

}

CompilationN/AN/ACompile OKScore: N/A

Testcase #14.169 ms23 MB + 492 KBAcceptedScore: 5

Testcase #24.127 ms23 MB + 496 KBAcceptedScore: 5

Testcase #34.222 ms23 MB + 504 KBAcceptedScore: 5

Testcase #44.145 ms23 MB + 508 KBAcceptedScore: 5

Testcase #56.053 ms24 MB + 316 KBAcceptedScore: 5

Testcase #66.059 ms24 MB + 296 KBAcceptedScore: 5

Testcase #769.922 ms39 MB + 808 KBAcceptedScore: 5

Testcase #869.644 ms38 MB + 660 KBAcceptedScore: 5

Testcase #9475.194 ms105 MB + 732 KBAcceptedScore: 5

Testcase #10477.608 ms108 MB + 724 KBAcceptedScore: 5

Testcase #11479.073 ms100 MB + 16 KBAcceptedScore: 5

Testcase #12478.795 ms103 MB + 256 KBAcceptedScore: 5

Testcase #136.294 ms24 MB + 320 KBAcceptedScore: 5

Testcase #146.192 ms24 MB + 376 KBAcceptedScore: 5

Testcase #1574.5 ms39 MB + 960 KBAcceptedScore: 5

Testcase #1675.457 ms40 MB + 584 KBAcceptedScore: 5

Testcase #17542.037 ms118 MB + 756 KBAcceptedScore: 5

Testcase #18541.837 ms118 MB + 868 KBAcceptedScore: 5

Testcase #19506.913 ms104 MB + 136 KBAcceptedScore: 5

Testcase #20528.151 ms112 MB + 308 KBAcceptedScore: 5


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