Skip to content

Commit 0d70909

Browse files
committed
NFLSOJ: 13105
「IOI2022互测8」树
1 parent 42ff84c commit 0d70909

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

NFLSOJ/13105.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @file 13105.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2022-06-04
5+
*
6+
* @copyright Copyright (c) 2022
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 22
20+
21+
int a[1 << maxn], cnt = 0;
22+
23+
void dfs(int p, int d, int c) {
24+
vector<int> lchain, rchain;
25+
lchain.push_back(p), rchain.push_back(p);
26+
for (int i = 1; i <= d; i++) lchain.push_back(lchain.back() << 1), rchain.push_back(rchain.back() << 1 | 1);
27+
a[lchain.back()] = a[rchain.back()] = c, lchain.pop_back(), rchain.pop_back();
28+
a[p] = a[lchain.back()] = a[rchain.back()] = ++cnt;
29+
if (lchain.back() != p) a[lchain.back() << 1 | 1] = a[rchain.back() << 1] = cnt;
30+
for (int i = 1, j = d - 2; i <= j; i++, j--) {
31+
int x = ++cnt;
32+
a[lchain[i]] = a[rchain[i]] = x, dfs(lchain[i] << 1 | 1, d - i - 1, x), dfs(rchain[i] << 1, d - i - 1, x);
33+
if (i == j) continue;
34+
a[lchain[j]] = a[rchain[j]] = x, dfs(lchain[j] << 1 | 1, d - j - 1, x), dfs(rchain[j] << 1, d - j - 1, x);
35+
}
36+
return;
37+
}
38+
39+
void solve(void) {
40+
int n, k;
41+
cin >> n >> k;
42+
if (n == 1) return cout << "1\n1 1 1" << endl, void();
43+
dfs(1, n, ++cnt);
44+
cout << cnt << endl;
45+
for (int i = 1; i < (1 << (n + 1)); i++) cout << a[i] << ' ';
46+
cout << endl;
47+
return;
48+
}
49+
50+
bool mem2;
51+
52+
int main() {
53+
ios::sync_with_stdio(false), cin.tie(nullptr);
54+
#ifndef LOCAL
55+
freopen("tree.in", "r", stdin), freopen("tree.out", "w", stdout);
56+
#endif
57+
#ifdef LOCAL
58+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
59+
#endif
60+
61+
int _ = 1;
62+
while (_--) solve();
63+
64+
#ifdef LOCAL
65+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
66+
#endif
67+
return 0;
68+
}

0 commit comments

Comments
 (0)