結果
| 問題 | No.566 だいたい完全二分木 | 
| コンテスト | |
| ユーザー |  okayunonaha | 
| 提出日時 | 2017-09-09 00:28:25 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 2,000 ms | 
| コード長 | 1,115 bytes | 
| コンパイル時間 | 1,613 ms | 
| コンパイル使用メモリ | 158,564 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-07 12:34:21 | 
| 合計ジャッジ時間 | 2,076 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 11 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
int k, m, root, l, r, cnt = 1, u;
bool used[100000];
void dfs(int left, int right) {
    if (cnt > u) return;
    if (left == right) return;
    if (right - left == 1) {
        if (left != 0 and !used[left] and !used[right]) {
            cout << left << " " << right << (cnt == u-1 ? "\n" : " ");
            cnt++;
        }
        else if (!used[right]) {
            cout << right << (cnt == u ? "\n" : " ");
            cnt++;
        }
        else if (left != 0 and !used[left]) {
            cout << left << (cnt == u ? "\n" : " ");
            cnt++;
        }
        used[left] = used[right] = true;
        return;
    }
    if (!used[(left + right) / 2]) {
        cout << (left + right) / 2 << (cnt == u ? "\n" : " ");
        cnt++;
    }
    used[(left + right) / 2] = true;
    dfs(left, (left + right) / 2);
    dfs((left + right) / 2 + 1, right);
}
int main() {
    cin >> k;
    u = pow(2, k) - 1;
    if (k == 2) {
        cout << 1 << " " << 2 << " " << 3 << endl;
        return 0;
    }
    else { 
        dfs(0, u);
    }
    return 0;
}
            
            
            
        