Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))

就是一欧拉路径

贴出邻接表欧拉路径

CODE

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, b[MAXN], c[MAXN], bin[MAXN<<1], tot;
int val[MAXN], deg[MAXN], stk[MAXN<<1], top;
int fir[MAXN], cnt=1, nxt[MAXN<<1], to[MAXN<<1];
inline void add(int u, int v){
	to[++cnt] = v; nxt[cnt] = fir[u]; fir[u] = cnt;
}
bool vis[MAXN];
void dfs(int u) {
	for(int &i = fir[u]; i; i = nxt[i])
		if(!vis[i>>1]) {
			vis[i>>1] = 1;
			dfs(to[i]);
		}
	stk[++top] = u;
}
int main () {
	scanf("%d", &n);
	for(int i = 1; i < n; ++i) scanf("%d", &b[i]), bin[++tot] = b[i];
	for(int i = 1; i < n; ++i) {
		scanf("%d", &c[i]), bin[++tot] = c[i];
		if(c[i] < b[i]) return puts("-1"), 0;
	}
	sort(bin + 1, bin + tot + 1);
	tot = unique(bin + 1, bin + tot + 1) - bin - 1;
	int now = 0;
	for(int i = 1; i < n; ++i) {
		int tempb = lower_bound(bin + 1, bin + tot + 1, b[i]) - bin; val[tempb] = b[i];
		int tempc = lower_bound(bin + 1, bin + tot + 1, c[i]) - bin; val[tempc] = c[i];
		b[i] = tempb, c[i] = tempc;
		if((++deg[b[i]])&1) ++now; else --now;
		if((++deg[c[i]])&1) ++now; else --now;	
		add(b[i], c[i]);
		add(c[i], b[i]);
	}
	if(!now) dfs(1);
	else if(now == 2) {
		for(int i = 1; i <= n; ++i)
			if(deg[i]&1) { dfs(i); break; }
	}
	if(top != n) puts("-1");
	else {
		for(int i = 1; i <= n; ++i)
			printf("%d%c", val[stk[i]], i == n ? '\n' : ' ');
	}
}
posted @ 2019-12-14 14:50  _Ark  阅读(76)  评论(0)    收藏  举报