Submission #74591902
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>struct dict_node {int value;struct dict_node* next[2];};void dict_set(struct dict_node** proot, int key, int value) {if (*proot == NULL) {*proot = malloc(sizeof(**proot));if (*proot == NULL) exit(2);**proot = (struct dict_node){ -1, { NULL, NULL }};}if (key == 0) {(*proot)->value = value;} else {dict_set(&(*proot)->next[key & 1], key >> 1, value);}}int dict_get(const struct dict_node* root, int key) {if (root == NULL) return -1;if (key == 0) return root->value;return dict_get(root->next[key & 1], key >> 1);}struct node_s;struct child_s {int element;struct node_s* next;};struct node_s {int id_num;int* id;int children_num;struct child_s* children;struct dict_node* children_indice;};
#include <stdio.h>
#include <stdlib.h>
struct dict_node {
int value;
struct dict_node* next[2];
};
void dict_set(struct dict_node** proot, int key, int value) {
if (*proot == NULL) {
*proot = malloc(sizeof(**proot));
if (*proot == NULL) exit(2);
**proot = (struct dict_node){ -1, { NULL, NULL }};
}
if (key == 0) {
(*proot)->value = value;
} else {
dict_set(&(*proot)->next[key & 1], key >> 1, value);
}
}
int dict_get(const struct dict_node* root, int key) {
if (root == NULL) return -1;
if (key == 0) return root->value;
return dict_get(root->next[key & 1], key >> 1);
}
struct node_s;
struct child_s {
int element;
struct node_s* next;
};
struct node_s {
int id_num;
int* id;
int children_num;
struct child_s* children;
struct dict_node* children_indice;
};
struct node_s root;
int N;
int x[312345], y[312345];
struct node_s* nodes[312345];
int cmp_int(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return (a > b) - (a < b);
}
int cmp_child(const void* x, const void* y) {
struct child_s a = *(const struct child_s*)x, b = *(const struct child_s*)y;
return (a.element > b.element) - (a.element < b.element);
}
int elem_exists = 0;
/* 行きがけ順で添え字を出力する */
void dfs(struct node_s* node) {
int i;
if (node->id_num > 0) {
qsort(node->id, node->id_num, sizeof(*node->id), cmp_int);
for (i = 0; i < node->id_num; i++) {
if (elem_exists) putchar(' ');
printf("%d", node->id[i]);
elem_exists = 1;
}
}
if (node->children_num > 0) {
qsort(node->children, node->children_num, sizeof(*node->children), cmp_child);
for (i = 0; i < node->children_num; i++) {
dfs(node->children[i].next);
}
}
}
int main(void) {
int i;
if (scanf("%d", &N) != 1) return 1;
for (i = 1; i <= N; i++) {
if (scanf("%d%d", &x[i], &y[i]) != 2) return 1;
}
nodes[0] = &root;
for (i = 1; i <= N; i++) {
struct node_s* parent = nodes[x[i]];
int child_idx = dict_get(parent->children_indice, y[i]);
if (child_idx >= 0) {
/* この整数がつく数列はすでにあるので、それを参照する */
nodes[i] = parent->children[child_idx].next;
} else {
/* 新しい数列を作成する */
nodes[i] = malloc(sizeof(*nodes[i]));
if (nodes[i] == NULL) return 2;
*nodes[i] = (struct node_s){ 0, NULL, 0, NULL, NULL };
/* 新しい数列をつく元の数列に接続する */
parent->children = realloc(parent->children, sizeof(*parent->children) * (parent->children_num + 1));
if (parent->children == NULL) return 2;
parent->children[parent->children_num] = (struct child_s){ y[i], nodes[i] };
dict_set(&parent->children_indice, y[i], parent->children_num);
parent->children_num++;
}
/* 数列に対応する添え字を追加する */
nodes[i]->id = realloc(nodes[i]->id, sizeof(*nodes[i]->id) * (nodes[i]->id_num + 1));
if (nodes[i]->id == NULL) return 2;
nodes[i]->id[nodes[i]->id_num++] = i;
}
dfs(&root);
putchar('\n');
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Sort Arrays |
| User | mikecat |
| Language | C23 (GCC 14.2.0) |
| Score | 450 |
| Code Size | 3108 Byte |
| Status | AC |
| Exec Time | 469 ms |
| Memory | 335928 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 450 / 450 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt, 01_test_42.txt, 01_test_43.txt, 01_test_44.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 1 ms | 1608 KiB |
| 00_sample_01.txt | AC | 0 ms | 1648 KiB |
| 00_sample_02.txt | AC | 0 ms | 1500 KiB |
| 01_test_00.txt | AC | 10 ms | 3120 KiB |
| 01_test_01.txt | AC | 64 ms | 9128 KiB |
| 01_test_02.txt | AC | 74 ms | 26712 KiB |
| 01_test_03.txt | AC | 149 ms | 46524 KiB |
| 01_test_04.txt | AC | 149 ms | 81728 KiB |
| 01_test_05.txt | AC | 290 ms | 132420 KiB |
| 01_test_06.txt | AC | 103 ms | 72624 KiB |
| 01_test_07.txt | AC | 372 ms | 212924 KiB |
| 01_test_08.txt | AC | 18 ms | 17992 KiB |
| 01_test_09.txt | AC | 444 ms | 273376 KiB |
| 01_test_10.txt | AC | 448 ms | 293812 KiB |
| 01_test_11.txt | AC | 458 ms | 304412 KiB |
| 01_test_12.txt | AC | 51 ms | 43220 KiB |
| 01_test_13.txt | AC | 468 ms | 306904 KiB |
| 01_test_14.txt | AC | 139 ms | 95088 KiB |
| 01_test_15.txt | AC | 465 ms | 307000 KiB |
| 01_test_16.txt | AC | 213 ms | 149204 KiB |
| 01_test_17.txt | AC | 469 ms | 306884 KiB |
| 01_test_18.txt | AC | 21 ms | 20316 KiB |
| 01_test_19.txt | AC | 469 ms | 306876 KiB |
| 01_test_20.txt | AC | 55 ms | 8356 KiB |
| 01_test_21.txt | AC | 55 ms | 8444 KiB |
| 01_test_22.txt | AC | 274 ms | 321612 KiB |
| 01_test_23.txt | AC | 171 ms | 174528 KiB |
| 01_test_24.txt | AC | 55 ms | 8944 KiB |
| 01_test_25.txt | AC | 112 ms | 66880 KiB |
| 01_test_26.txt | AC | 302 ms | 326476 KiB |
| 01_test_27.txt | AC | 167 ms | 169912 KiB |
| 01_test_28.txt | AC | 57 ms | 8932 KiB |
| 01_test_29.txt | AC | 306 ms | 268860 KiB |
| 01_test_30.txt | AC | 308 ms | 331080 KiB |
| 01_test_31.txt | AC | 199 ms | 172476 KiB |
| 01_test_32.txt | AC | 59 ms | 9160 KiB |
| 01_test_33.txt | AC | 327 ms | 286020 KiB |
| 01_test_34.txt | AC | 308 ms | 335928 KiB |
| 01_test_35.txt | AC | 182 ms | 172036 KiB |
| 01_test_36.txt | AC | 68 ms | 9076 KiB |
| 01_test_37.txt | AC | 349 ms | 307684 KiB |
| 01_test_38.txt | AC | 314 ms | 331840 KiB |
| 01_test_39.txt | AC | 192 ms | 169340 KiB |
| 01_test_40.txt | AC | 109 ms | 14824 KiB |
| 01_test_41.txt | AC | 354 ms | 309116 KiB |
| 01_test_42.txt | AC | 317 ms | 330508 KiB |
| 01_test_43.txt | AC | 215 ms | 175624 KiB |
| 01_test_44.txt | AC | 1 ms | 1620 KiB |