提交记录 6872


用户 题目 状态 得分 用时 内存 语言 代码长度
lizongru 2002. 【NOIP2018】旅行(加强版) Runtime Error 30 1.489 ms 5528 KB C++ 2.51 KB
提交时间 评测时间
2018-11-11 18:20:50 2020-08-01 00:52:07
#include<bits/stdc++.h>
using namespace std;
inline void read(int &x){
	x = 0;
	int p = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-')p = -1;
		c = getchar();
	}
	while(isdigit(c)){
		x = (x << 1) + (x << 3) + (c ^ '0');
		c = getchar();
	}
	x *= p;
}
const int maxn = 5050;
vector<int>vec[maxn];
int n, m;
namespace sub1{
	vector<int>ans;
	bool vis[maxn];
	inline void dfs(int u){
		vis[u] = 1;
		ans.push_back(u);
		for(register int i = 0, sz = vec[u].size(); i < sz; ++i){
			int v = vec[u][i];
			if(!vis[v])dfs(v);
		}
	}
	inline void solve(){
		memset(vis, 0, sizeof(vis));
		dfs(1);
		for(register int i = 0, sz = ans.size(); i < sz; ++i){
			printf("%d ", ans[i]);
		}
	}
}
namespace sub2{
	stack<int>sta;
	vector<int>cir, ans;
	bool flag = 0, cjump = 0, nowjump = 0, entcir = 0;
	bool vis[maxn], iscir[maxn];
	int beg, nxt[10], tot = 0;
	inline void circle(int fir){
		while(!sta.empty() && sta.top() != fir){
			int u = sta.top();
			cir.push_back(u);
			iscir[u] = 1;
			sta.pop();
		}
		cir.push_back(fir);
		iscir[fir] = 1;
		sta.pop();
	}
	inline void get_cir(int u, int fa){
		vis[u] = 1;
		sta.push(u);
		for(register int i = 0, sz = vec[u].size(); i < sz; ++i){
			int v = vec[u][i];
			if(v == fa)continue;
			if(flag)return ;
			if(vis[v]){
				circle(v);
				flag = 1;
				return ;
			}
			get_cir(v, u);
		}
		sta.pop();
	}
	inline void dfs(int u){
		//cerr<<u<<endl;
		vis[u] = 1;
		ans.push_back(u);
		if((!entcir) && iscir[u]){
			entcir = 1;
			beg = u;
			for(register int i = 0, sz = vec[u].size(); i < sz; ++i){
				if(iscir[vec[u][i]])nxt[++tot] = vec[u][i];
			}
			if(nxt[1] > nxt[2])swap(nxt[1], nxt[2]);
			cjump = 1;
		}
		for(register int i = 0, sz = vec[u].size(); i < sz; ++i){
			int v = vec[u][i];
			if(cjump && iscir[v] && v > nxt[2]){
				//cerr<<u<<":::"<<nxt[2]<<endl;
				cjump = 0, nowjump = 1;
				continue;
			}
			if(!vis[v])dfs(v);
		}
		if(nowjump){
			if(u != beg)return ;
			else nowjump = 0;
		}
	}
	inline void solve(){
		memset(vis, 0, sizeof(vis));
		get_cir(1, 0);
		/*for(register int i = 0, sz = cir.size(); i < sz; ++i){
			printf("%d ", cir[i]);
		}*/
		//puts("");
		memset(vis, 0, sizeof(vis));
		dfs(1);
		for(register int i = 0, sz = ans.size(); i < sz; ++i){
			printf("%d ", ans[i]);
		}
	}
}
int main(){
	read(n), read(m);
	int u, v;
	for(register int i = 1; i <= m; ++i){
		read(u), read(v);
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	for(register int i = 1; i <= n; ++i){
		sort(vec[i].begin(), vec[i].end());
	}
	if(m == n - 1)sub1::solve();
	else sub2::solve();
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #155.96 us164 KBAcceptedScore: 5

Testcase #258.52 us164 KBAcceptedScore: 5

Testcase #381.3 us168 KBAcceptedScore: 5

Testcase #481.78 us168 KBAcceptedScore: 5

Testcase #51.489 ms572 KBAcceptedScore: 5

Testcase #61.488 ms552 KBAcceptedScore: 5

Testcase #7163.19 us1 MB + 84 KBRuntime ErrorScore: 0

Testcase #8186.16 us1 MB + 268 KBRuntime ErrorScore: 0

Testcase #969.77 us352 KBRuntime ErrorScore: 0

Testcase #10175.05 us1 MB + 188 KBRuntime ErrorScore: 0

Testcase #11158.53 us1 MB + 44 KBRuntime ErrorScore: 0

Testcase #12163.84 us1 MB + 100 KBRuntime ErrorScore: 0

Testcase #13704.63 us596 KBRuntime ErrorScore: 0

Testcase #14719.78 us668 KBRuntime ErrorScore: 0

Testcase #15167.02 us1 MB + 128 KBRuntime ErrorScore: 0

Testcase #16116.08 us748 KBRuntime ErrorScore: 0

Testcase #171.105 ms1 MB + 516 KBRuntime ErrorScore: 0

Testcase #18129.77 us832 KBRuntime ErrorScore: 0

Testcase #19717.35 us5 MB + 408 KBRuntime ErrorScore: 0

Testcase #20307.79 us2 MB + 264 KBRuntime ErrorScore: 0


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