bzoj3033 太鼓达人 欧拉回路

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=3033

题解

首先根据直觉,第一问的答案肯定是 \(2^k\),也就是所有长度为 \(k\) 的二进制串。(并不会证明

然后把长度为 \(2^{k-1}\) 的数看成一个点,最后一位是边上的权值,连接到接上最后一位以后的最后 \(k-1\) 位。

然后最后答案一定是这个图的一个欧拉回路。为了保证字典序最小,所以每次优先走 \(0\),然后走 \(1\)


时间复杂度 \(O(n\log n)\)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int K = 11 + 2;
const int N = (1 << 11) + 7;

int n, k, cnt;
char ans[N];
bool vis[N << 1];

struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }

inline void dfs(int x) {
	for fec(i, x, y) if (!vis[i]) {
		vis[i] = 1;
		dfs(y);
	}
	ans[++cnt] = x;
}

inline void work() {
	for (int i = 0; i <= (n >> 1); ++i) addedge(i, (n >> 1) & ((i << 1) | 1)), addedge(i, (n >> 1) & (i << 1));
	dfs(0);
//	dbg("cnt = %d\n", cnt);
	printf("%d ", n + 1);
	--cnt;
	for (int i = 0; i < k; ++i) putchar('0' + ((ans[cnt] >> i) & 1));
	--cnt;
	while (cnt >= k) putchar('0' + (ans[cnt--] & 1));
	puts("");
}

inline void init() {
	read(k);
	n = (1 << k) - 1;
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-11-04 21:59  hankeke303  阅读(202)  评论(0编辑  收藏  举报