#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> #include <set> #include <map> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) int debug = 0; int n=0; map<int, VI> deg_to_node_ids; map<int, int> node_to_deg; void dfs(int k, int cnt) { if (k <= 1) { return; } auto &t = deg_to_node_ids[k]; bool no_need_recursive = !t.empty(); int to_add = cnt - t.size(); REP(i, to_add) { t.PB(++n); } if (no_need_recursive) { return; } int left = k/2; int right = k - left; assert(left <= right); if (left == right) { dfs(left, 2); } else { dfs(left, 1); dfs(right, 1); } } void print_node(int from) { int k = node_to_deg[from]; int left_id, right_id; if (k == 0) { left_id = -1; right_id = -1; } else if (k == 1) { left_id = n; right_id = -1; } else { int left = k/2; int right = k-left; assert(left <= right); if (left == 1) { left_id = n; right_id = deg_to_node_ids[right][0]; } else if (left == right) { left_id = deg_to_node_ids[left][0]; right_id = deg_to_node_ids[right][1]; } else { left_id = deg_to_node_ids[left][0]; right_id = deg_to_node_ids[right][0]; } } printf("%d %d\n", left_id, right_id); } int main() { int k; scanf("%d", &k); dfs(k, 1); deg_to_node_ids[1].PB(++n); deg_to_node_ids[0].PB(++n); assert(n <= 100); for (auto const& [key, val] : deg_to_node_ids) { assert(val.size() <= 2); REP(i, val.size()) { node_to_deg[val[i]] = key; if (debug) { printf("node_id: %d deg: %d\n", val[i], key); } } } printf("%d\n", n); FOR(from, 1, n) { print_node(from); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> #include <set> #include <map> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) int debug = 0; int n=0; map<int, VI> deg_to_node_ids; map<int, int> node_to_deg; void dfs(int k, int cnt) { if (k <= 1) { return; } auto &t = deg_to_node_ids[k]; bool no_need_recursive = !t.empty(); int to_add = cnt - t.size(); REP(i, to_add) { t.PB(++n); } if (no_need_recursive) { return; } int left = k/2; int right = k - left; assert(left <= right); if (left == right) { dfs(left, 2); } else { dfs(left, 1); dfs(right, 1); } } void print_node(int from) { int k = node_to_deg[from]; int left_id, right_id; if (k == 0) { left_id = -1; right_id = -1; } else if (k == 1) { left_id = n; right_id = -1; } else { int left = k/2; int right = k-left; assert(left <= right); if (left == 1) { left_id = n; right_id = deg_to_node_ids[right][0]; } else if (left == right) { left_id = deg_to_node_ids[left][0]; right_id = deg_to_node_ids[right][1]; } else { left_id = deg_to_node_ids[left][0]; right_id = deg_to_node_ids[right][0]; } } printf("%d %d\n", left_id, right_id); } int main() { int k; scanf("%d", &k); dfs(k, 1); deg_to_node_ids[1].PB(++n); deg_to_node_ids[0].PB(++n); assert(n <= 100); for (auto const& [key, val] : deg_to_node_ids) { assert(val.size() <= 2); REP(i, val.size()) { node_to_deg[val[i]] = key; if (debug) { printf("node_id: %d deg: %d\n", val[i], key); } } } printf("%d\n", n); FOR(from, 1, n) { print_node(from); } return 0; } |