diff --git a/.idea/editor.xml b/.idea/editor.xml
index 14cdacd..e956a9f 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -28,6 +28,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -205,6 +221,7 @@
+
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1034548..2992a5b 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -33,6 +33,7 @@
+
@@ -40,16 +41,19 @@
+
+
+
@@ -61,11 +65,14 @@
+
+
+
@@ -73,6 +80,7 @@
+
@@ -83,15 +91,19 @@
+
+
+
+
@@ -99,6 +111,7 @@
+
@@ -107,6 +120,8 @@
+
+
@@ -121,6 +136,7 @@
+
@@ -128,6 +144,8 @@
+
+
@@ -135,50 +153,29 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -212,9 +209,8 @@
-
-
+
@@ -308,6 +304,15 @@
"CMake 应用程序.cs101__dh_20260923_11_fibonacci.executor": "Run",
"CMake 应用程序.cs101__sh_20260923_14_balloons.executor": "Run",
"CMake 应用程序.cs101__stage_2__dh_11_03_jump_rope.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_12_01_variance.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_12_03_tumor_detection.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_12_04_day_delta.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_12_05_iter_hui.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_12_big_binary_addition.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_13_01_max_points.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_13_02_tumor_again.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_13_05_books.executor": "Run",
+ "CMake 应用程序.cs101__stage_2__dh_13_06_nums_in_string.executor": "Debug",
"CMake 应用程序.dp__TJOI2007_walk_segments_dp.executor": "Run",
"CMake 应用程序.dp__luogu_U434718_eagle_egg.executor": "Run",
"CMake 应用程序.dsa__ch2_linear_list__P2629_good_news.executor": "Run",
@@ -317,6 +322,13 @@
"CMake 应用程序.dsa__ch3_stack_queue__dsa_0301_sliding_window_SLOW.executor": "Run",
"CMake 应用程序.dsa__ch3_stack_queue__dsa_0302_calculator.executor": "Run",
"CMake 应用程序.dsa__ch3_stack_queue__dsa_0306_stack_operation.executor": "Run",
+ "CMake 应用程序.dsa__ch4_string__dsa_0403_within_string.executor": "Run",
+ "CMake 应用程序.dsa__ch4_string__dsa_0404_string_loop.executor": "Run",
+ "CMake 应用程序.dsa__ch4_string__dsa_0406_valid_string.executor": "Run",
+ "CMake 应用程序.dsa__ch4_string__luogu_template_kmp_P3375.executor": "Run",
+ "CMake 应用程序.dsa__ch5_binary_tree__dsa_0510_reconstruct_tree.executor": "Run",
+ "CMake 应用程序.dsa__ch5_binary_tree__luogu_P4715_matches.executor": "Run",
+ "CMake 应用程序.dsa__ch5_binary_tree__luogu_T750119_bin_tree_iter.executor": "Run",
"CMake 应用程序.dsa__linear_list__dsa_0201_dictionary.executor": "Run",
"CMake 应用程序.dsa__linear_list__dsa_0202_card_stacking.executor": "Run",
"CMake 应用程序.dsa__linear_list__dsa_0204_prefix_cycle.executor": "Run",
@@ -364,11 +376,16 @@
"codeWithMe.voiceChat.enabledByDefault": "false",
"deletionFromPopupRequiresConfirmation": "false",
"git-widget-placeholder": "main",
- "last_opened_file_path": "/Users/roger/coding/exercises/cs101",
+ "last_opened_file_path": "/Users/roger/coding/exercises/cs101/stage_2",
+ "leon.ai.report.branch": "main",
+ "leon.t.first": "low",
+ "leon.t.source": "auto",
+ "leon.t.target": "en",
"node.js.detected.package.jshint": "true",
"node.js.selected.package.jshint": "",
"nodejs_package_manager_path": "npm",
"org.rust.first.attach.projects": "true",
+ "python.packages.toolwindow.project.migrated.to.right.top.v2": "true",
"settings.editor.selected.configurable": "preferences.keymap"
}
}]]>
@@ -377,11 +394,11 @@
+
-
@@ -391,7 +408,7 @@
-
+
@@ -602,6 +619,61 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -767,6 +839,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1050,6 +1157,8 @@
+
+
@@ -1066,14 +1175,25 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1081,12 +1201,18 @@
-
-
+
+
+
+
+
+
+
+
@@ -1094,21 +1220,20 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1147,8 +1272,8 @@
-
+
@@ -1184,6 +1309,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_12_05_iter_hui",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__dsa_0301_sliding_window_SLOW",
@@ -1191,6 +1323,55 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch4_string__dsa_0404_string_loop",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_12_03_tumor_detection",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_12_01_variance",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_13_02_tumor_again",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch4_string__dsa_0406_valid_string",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch5_binary_tree__dsa_0510_reconstruct_tree",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_13_01_max_points",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.luogu_P1160_biodir_linked_list",
@@ -1226,6 +1407,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch4_string__dsa_0403_within_string",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__USACO12MARs_flowerpot",
@@ -1233,6 +1421,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_13_05_books",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.pojcs101_T27373_biggest_combination_t2",
@@ -1247,6 +1442,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_12_04_day_delta",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "C/C++ 文件.dh_05_05_pair_of_shit_nums_ANALYSIS.cpp"
@@ -1266,6 +1468,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch4_string__luogu_template_kmp_P3375",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.dp__TJOI2007_walk_segments_dp",
@@ -1273,6 +1482,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__stage_2__dh_13_06_nums_in_string",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__dsa_0306_stack_operation",
@@ -1301,6 +1517,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch5_binary_tree__luogu_T750119_bin_tree_iter",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.NOIP2003J_bigint",
@@ -1328,9 +1551,16 @@
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch5_binary_tree__luogu_P4715_matches",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
}
],
- "modificationCount": 192
+ "modificationCount": 239
}]]>
@@ -1370,7 +1600,8 @@
-
+
+
@@ -1425,11 +1656,6 @@
99
-
- file://$PROJECT_DIR$/dsa/ch3_stack_queue/dsa_0302_calculator.cpp
- 102
-
-
file://$PROJECT_DIR$/dsa/ch2_linear_list/SDOI2007_queue.cpp
77
@@ -1460,6 +1686,26 @@
46
+
+ file://$PROJECT_DIR$/cs101/stage_2/dh_12_01_variance.cpp
+ 11
+
+
+
+ file://$PROJECT_DIR$/dsa/ch4_string/luogu_template_kmp_P3375.cpp
+ 20
+
+
+
+ file://$PROJECT_DIR$/dsa/ch5_binary_tree/luogu_P4715_matches.cpp
+ 54
+
+
+
+ file://$PROJECT_DIR$/dsa/ch5_binary_tree/dsa_0510_reconstruct_tree.cpp
+ 34
+
+
diff --git a/_others/others_test.c b/_others/others_test.c
index c13bdee..a6a961b 100644
--- a/_others/others_test.c
+++ b/_others/others_test.c
@@ -1,21 +1,22 @@
#include
-#define _NOT_USED \
-'''
-
int main() {
- int a, b;
- scanf("%d%d", &a, &b);
- printf("%d", a + b);
+ int number = 0, logo = 0;
+ scanf("%d", &number);
+ for (int wait_test = 1; wait_test <= number; wait_test++) {
+ while (logo == 0) {
+ int same = wait_test;
+ while (same >= 67) {
+ int a = same % 100;
+ if (a == 67) {
+ printf("%d\n", wait_test);
+ logo++;
+ break;
+ }
+ same /= 10;
+ }
+ }
+ logo--;
+ }
return 0;
}
-
-#define _NOT_USED \
-'''
-
-#define _NOT_USED /*
-
-a, b = map(int, input().split())
-print(a + b)
-
-#*/
diff --git a/_others/others_test_cpp.cpp b/_others/others_test_cpp.cpp
index 076c93f..ea880fe 100644
--- a/_others/others_test_cpp.cpp
+++ b/_others/others_test_cpp.cpp
@@ -1,29 +1,48 @@
-#include
+#include
+#include
+#include
+#include
-using namespace std;
+constexpr int MAX_N = 1001, MAX_T = 1001;
-constexpr float ALLOWED_DIFF = 1e-6;
+struct Attendee {
+ int enter_time = 0, exit_time = 0;
+};
-std::tuple sqrt_custom(double x) {
- double est_prev = -1, est_curr = x;
- int rounds = 0;
- while (abs(est_curr - est_prev) > ALLOWED_DIFF) {
- est_prev = est_curr;
- // est_curr = 0.5 * (est_prev + x / est_prev);
- est_curr = x / (2 * est_curr) + 0.5 * est_curr;
- rounds++;
+
+int get_max_attendees_amount(std::vector attendees_schedule) {
+ std::array bucket{};
+ for (const auto [enter_time, exit_time] : attendees_schedule) {
+ for (int t = enter_time; t < exit_time; t++) {
+ bucket[t]++;
+ }
}
- return std::make_tuple(rounds, est_curr);
+ return *std::max_element(bucket.begin(), bucket.end());
}
+
int main() {
- std::ios::sync_with_stdio(false);
+ std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
+
+ std::vector attendees;
+ char temp_comma;
int x;
- while (std::cin >> x) {
- auto [rounds, ans] = sqrt_custom(x);
- std::cout << rounds << " " << std::fixed << std::setprecision(2) << ans << std::endl;
+ while (true) {
+ std::cin >> x >> temp_comma;
+ attendees.push_back({x, -1});
+ if (temp_comma != ',') {
+ std::cin.unget();
+ break;
+ }
}
+ for (int i = 0; i < attendees.size() - 1; i++) {
+ std::cin >> attendees[i].exit_time >> temp_comma;
+ }
+ std::cin >> attendees[attendees.size() - 1].exit_time;
+
+ std::cout << attendees.size() << ' ' << get_max_attendees_amount(attendees) << std::endl;
+
return 0;
}
diff --git a/cs101/stage_2/dh_12_01_variance.cpp b/cs101/stage_2/dh_12_01_variance.cpp
new file mode 100644
index 0000000..dc76ec5
--- /dev/null
+++ b/cs101/stage_2/dh_12_01_variance.cpp
@@ -0,0 +1,35 @@
+#include
+#include
+#include
+#include
+
+double calc_average(const std::vector& nums) {
+ return std::accumulate(nums.begin(), nums.end(), 0.0) / static_cast(nums.size());
+}
+
+double calc_variance(const std::vector& nums) {
+ double avg = calc_average(nums);
+ double accumulated = std::accumulate(
+ nums.begin(), nums.end(),
+ 0.0,
+ [avg](double prev, double x) {
+ return prev + (x - avg) * (x - avg);
+ }
+ );
+ return accumulated / static_cast(nums.size());
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+ std::vector nums(n);
+ for (int i = 0; i < n; i++) { std::cin >> nums[i]; }
+
+ std::cout << std::fixed << std::setprecision(5)
+ << calc_average(nums) << " " << calc_variance(nums) << std::endl;
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_12_02_count_chars.cpp b/cs101/stage_2/dh_12_02_count_chars.cpp
new file mode 100644
index 0000000..8e511a2
--- /dev/null
+++ b/cs101/stage_2/dh_12_02_count_chars.cpp
@@ -0,0 +1,20 @@
+#include
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::string s;
+ std::getline(std::cin, s);
+
+ int uppers_amount = 0, lowers_amount = 0, digits_amount = 0;
+
+ for (char c : s) {
+ if (std::isupper(c)) { uppers_amount++; }
+ if (std::islower(c)) { lowers_amount++; }
+ if (std::isdigit(c)) { digits_amount++; }
+ }
+ std::cout << uppers_amount << " " << lowers_amount << " " << digits_amount << std::endl;
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_12_03_tumor_detection.cpp b/cs101/stage_2/dh_12_03_tumor_detection.cpp
new file mode 100644
index 0000000..e8c076f
--- /dev/null
+++ b/cs101/stage_2/dh_12_03_tumor_detection.cpp
@@ -0,0 +1,66 @@
+#include
+#include
+#include
+
+class Vector2 {
+public:
+ int x = 0, y = 0;
+
+ constexpr Vector2 operator+(const Vector2 other) const {
+ return {x + other.x, y + other.y};
+ }
+};
+
+using TumorGraphLine = std::vector;
+using TumorGraph = std::vector;
+
+constexpr int THRESHOLD = 50;
+constexpr std::array DIRS = {Vector2{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
+
+bool is_border(const TumorGraph& tumor_graph, Vector2 pos) {
+ if (tumor_graph[pos.x][pos.y] > THRESHOLD) { return false; }
+ for (const auto dir : DIRS) {
+ Vector2 new_pos = pos + dir;
+ if (tumor_graph[new_pos.x][new_pos.y] > THRESHOLD) { return true; }
+ }
+ return false;
+}
+
+int calc_area(const TumorGraph& tumor_graph) {
+ int area = 0;
+ for (const auto& line : tumor_graph) {
+ for (const auto val : line) {
+ if (val <= THRESHOLD) { area++; }
+ }
+ }
+ return area;
+}
+
+int calc_perimeter(const TumorGraph& tumor_graph) {
+ int perimeter = 0;
+ for (int i = 1; i < tumor_graph.size() - 1; i++) {
+ for (int j = 1; j < tumor_graph.size() - 1; j++) {
+ if (is_border(tumor_graph, {i, j})) { perimeter++; }
+ }
+ }
+ return perimeter;
+}
+
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+
+ TumorGraph tumor_graph(n + 2, TumorGraphLine(n + 2, 127));
+ for (int i = 1; i <= n; i++) {
+ for (int j = 1; j <= n; j++) {
+ std::cin >> tumor_graph[i][j];
+ }
+ }
+ std::cout << calc_area(tumor_graph) << ' ' << calc_perimeter(tumor_graph) << std::endl;
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_12_04_day_delta.cpp b/cs101/stage_2/dh_12_04_day_delta.cpp
new file mode 100644
index 0000000..8cd8096
--- /dev/null
+++ b/cs101/stage_2/dh_12_04_day_delta.cpp
@@ -0,0 +1,54 @@
+#include
+#include
+
+constexpr int DAYS_OF_MONTHS[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+
+constexpr bool is_leap(int year) {
+ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
+}
+
+constexpr int get_days_of_month(int year, int month) {
+ if (month == 2) { return is_leap(year) ? 29 : 28; }
+ return DAYS_OF_MONTHS[month];
+}
+
+constexpr int get_days_of_year(int year) {
+ return is_leap(year) ? 366 : 365;
+}
+
+constexpr int calc_passed_days(int year, int month, int day) {
+ int passed_days = 0;
+ for (int m = 1; m < month; m++) {
+ passed_days += get_days_of_month(year, m);
+ }
+ return passed_days + day;
+}
+
+constexpr int calc_remaining_days(int year, int month, int day) {
+ return get_days_of_year(year) - calc_passed_days(year, month, day);
+}
+
+constexpr int calc_days_delta(int year1, int month1, int day1,
+ int year2, int month2, int day2) {
+ int days_delta = 0;
+ if (year1 == year2) {
+ return calc_passed_days(year2, month2, day2) - calc_passed_days(year1, month1, day1);
+ }
+ for (int y = year1 + 1; y < year2; y++) {
+ days_delta += get_days_of_year(y);
+ }
+ return days_delta
+ + calc_remaining_days(year1, month1, day1)
+ + calc_passed_days(year2, month2, day2);
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int year1, month1, day1, year2, month2, day2;
+ std::cin >> year1 >> month1 >> day1 >> year2 >> month2 >> day2;
+ std::cout << calc_days_delta(year1, month1, day1, year2, month2, day2) << std::endl;
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_12_05_iter_hui.cpp b/cs101/stage_2/dh_12_05_iter_hui.cpp
new file mode 100644
index 0000000..af911d3
--- /dev/null
+++ b/cs101/stage_2/dh_12_05_iter_hui.cpp
@@ -0,0 +1,73 @@
+#include
+#include
+
+class Vector2 {
+public:
+ int x = 0, y = 0;
+
+ constexpr Vector2 operator+(Vector2 other) const {
+ return {x + other.x, y + other.y};
+ }
+
+ constexpr Vector2 operator+=(Vector2 other) {
+ x += other.x, y += other.y;
+ return *this;
+ }
+
+ constexpr Vector2 rotated(bool clockwise = false) const {
+ if (clockwise) {
+ return {y, -x}; // (2,1) -> (1,-2)
+ }
+ return {-y, x}; // (2,1) -> (-1,2)
+ }
+};
+
+struct GridElement {
+ int value = -1;
+ bool wall = false;
+};
+
+using Grid = std::vector>;
+
+Grid get_initial_grid(int h, int w) {
+ Grid grid = Grid(h + 2, std::vector(w + 2));
+ grid[0] = grid[h + 1] = std::vector(w + 2, {.wall = true});
+ for (int i = 1; i < h + 2; i++) {
+ grid[i][0].wall = grid[i][w + 1].wall = true;
+ }
+ return grid;
+}
+
+void move(const Grid& grid, Vector2& pos, Vector2& dir) {
+ Vector2 new_pos = pos + dir;
+ if (grid[new_pos.x][new_pos.y].wall) {
+ dir = dir.rotated(true);
+ }
+ pos += dir;
+}
+
+void iter_and_print_grid(Grid& grid) {
+ Vector2 pos = {1, 1}, dir = {0, 1};
+ for (int _ = 0; _ < (grid.size() - 2) * (grid[0].size() - 2); _++) {
+ std::cout << grid[pos.x][pos.y].value << std::endl;
+ grid[pos.x][pos.y].wall = true;
+ move(grid, pos, dir);
+ }
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int h, w;
+ std::cin >> h >> w;
+ Grid grid = get_initial_grid(h, w);
+ for (int i = 1; i <= h; i++) {
+ for (int j = 1; j <= w; j++) {
+ std::cin >> grid[i][j].value;
+ }
+ }
+ iter_and_print_grid(grid);
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_12_06_big_binary_addition.cpp b/cs101/stage_2/dh_12_06_big_binary_addition.cpp
new file mode 100644
index 0000000..7acfe04
--- /dev/null
+++ b/cs101/stage_2/dh_12_06_big_binary_addition.cpp
@@ -0,0 +1,47 @@
+#include
+#include
+
+using BigBin = std::vector;
+
+BigBin plus(BigBin a, const BigBin& b) {
+ if (a.size() < b.size()) { return plus(b, a); }
+ for (int i = 0; i < b.size(); i++) {
+ a[i] += b[i];
+ }
+ a.push_back(0);
+ for (int i = 1; i < a.size(); i++) {
+ if (a[i - 1] > 1) { a[i - 1] -= 2, a[i]++; }
+ }
+ while (a.back() == 0) { a.pop_back(); }
+ return a;
+}
+
+BigBin parse_string(const std::string s) {
+ BigBin big_bin(s.size());
+ for (int i = 0; i < s.size(); i++) {
+ big_bin[i] = s[s.size() - i - 1] - '0';
+ }
+ return big_bin;
+}
+
+std::ostream& operator<<(std::ostream& os, const BigBin& big_bin) {
+ for (int i = big_bin.size() - 1; i >= 0; i--) {
+ os << big_bin[i];
+ }
+ return os;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::string s1, s2;
+ std::cin >> n;
+ for (int _ = 0; _ < n; _++) {
+ std::cin >> s1 >> s2;
+ std::cout << plus(parse_string(s1), parse_string(s2)) << std::endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_13_01_max_points.cpp b/cs101/stage_2/dh_13_01_max_points.cpp
new file mode 100644
index 0000000..88ad326
--- /dev/null
+++ b/cs101/stage_2/dh_13_01_max_points.cpp
@@ -0,0 +1,47 @@
+#include
+#include
+#include
+
+constexpr int GRID_SIZE = 101;
+std::array, GRID_SIZE> has_point, is_invalid;
+
+void add_point(int x, int y) {
+ has_point[x][y] = true;
+ for (int i = 0; i <= x; i++) {
+ for (int j = 0; j <= y; j++) {
+ if (i == x && j == y) { break; }
+ is_invalid[i][j] = true;
+ }
+ }
+}
+
+void print_ans() {
+ bool first = true;
+ for (int i = 0; i < GRID_SIZE; i++) {
+ for (int j = 0; j < GRID_SIZE; j++) {
+ if (has_point[i][j] && !is_invalid[i][j]) {
+ if (!first) { std::cout << ','; }
+ std::cout << '(' << i << ',' << j << ')';
+ first = false;
+ }
+ }
+ }
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+
+ for (int _ = 0; _ < n; _++) {
+ int x, y;
+ std::cin >> x >> y;
+ add_point(x, y);
+ }
+
+ print_ans();
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_13_01_max_points_WRONG.cpp b/cs101/stage_2/dh_13_01_max_points_WRONG.cpp
new file mode 100644
index 0000000..3488651
--- /dev/null
+++ b/cs101/stage_2/dh_13_01_max_points_WRONG.cpp
@@ -0,0 +1,60 @@
+#include
+#include
+#include
+
+constexpr int GRID_SIZE = 101;
+std::array, GRID_SIZE> grid;
+std::array row_maxs, col_maxs;
+bool has_0_0 = false;
+
+void init_maxs() {
+ for (int i = 0; i < GRID_SIZE; i++) {
+ for (int j = GRID_SIZE - 1; j >= 0; j--) {
+ if (grid[i][j]) {
+ row_maxs[i] = j;
+ break;
+ }
+ }
+ }
+ for (int j = 0; j < GRID_SIZE; j++) {
+ for (int i = GRID_SIZE - 1; i >= 0; i--) {
+ if (grid[i][j]) {
+ col_maxs[j] = i;
+ break;
+ }
+ }
+ }
+}
+
+void print_ans() {
+ bool first = true;
+ for (int i = 0; i < GRID_SIZE; i++) {
+ int j = row_maxs[i];
+ if (col_maxs[j] == i) {
+ if (i == 0 && j == 0 && !has_0_0) { continue; }
+ if (!first) { std::cout << ','; }
+ std::cout << '(' << i << ',' << j << ')';
+ first = false;
+ }
+ }
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+
+ for (int _ = 0; _ < n; _++) {
+ int x, y;
+ std::cin >> x >> y;
+ grid[x][y] = true;
+ if (x == 0 && y == 0) { has_0_0 = true; }
+ }
+
+ init_maxs();
+ print_ans();
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_13_02_tumor_again.cpp b/cs101/stage_2/dh_13_02_tumor_again.cpp
new file mode 100644
index 0000000..a547cd7
--- /dev/null
+++ b/cs101/stage_2/dh_13_02_tumor_again.cpp
@@ -0,0 +1,35 @@
+#include
+
+using Coord = std::pair;
+
+constexpr int INF = 0x3f3f3f3f;
+Coord top_left = {INF, INF}, bottom_right = {-INF, -INF};
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < n; j++) {
+ int x;
+ std::cin >> x;
+ if (x != 0) { continue; }
+ top_left = std::min(top_left, {i, j});
+ bottom_right = std::max(bottom_right, {i, j});
+ // top_left.first = std::min(top_left.first,i);
+ // top_left.second = std::min(top_left.second,j);
+ // bottom_right.first = std::max(bottom_right.first,i);
+ // bottom_right.second = std::max(bottom_right.second,j);
+ }
+ }
+ std::cout
+ << std::max(
+ (bottom_right.first - top_left.first - 1) * (bottom_right.second - top_left.second - 1)
+ , 0)
+ << std::endl;
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_13_05_books.cpp b/cs101/stage_2/dh_13_05_books.cpp
new file mode 100644
index 0000000..c6dd938
--- /dev/null
+++ b/cs101/stage_2/dh_13_05_books.cpp
@@ -0,0 +1,36 @@
+#include
+#include
+#include
+#include
+
+constexpr int AUTHORS_AMOUNT = 26;
+std::array, AUTHORS_AMOUNT> books_of_authors;
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int books_amount;
+ std::cin >> books_amount;
+
+ for (int _ = 0; _ < books_amount; _++) {
+ int book_id;
+ std::string authors;
+ std::cin >> book_id >> authors;
+ for (char c : authors) {
+ books_of_authors[c - 'A'].push_back(book_id);
+ }
+ }
+
+ auto best_author = std::max_element(books_of_authors.begin(), books_of_authors.end(),
+ [](const auto& a, const auto& b) {
+ return a.size() < b.size();
+ });
+ std::cout << static_cast(best_author - books_of_authors.begin() + 'A') << std::endl
+ << best_author->size() << std::endl;
+ for (auto book : *best_author) {
+ std::cout << book << std::endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/stage_2/dh_13_06_nums_in_string.cpp b/cs101/stage_2/dh_13_06_nums_in_string.cpp
new file mode 100644
index 0000000..6e3e171
--- /dev/null
+++ b/cs101/stage_2/dh_13_06_nums_in_string.cpp
@@ -0,0 +1,38 @@
+#include
+#include
+#include
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::vector numbers;
+ int chr;
+ bool in_number = false;
+ while ((chr = std::getchar()) != EOF && chr != '\n') {
+ char c = static_cast(chr);
+ if (!isdigit(c)) {
+ in_number = false;
+ continue;
+ }
+ if (in_number) {
+ numbers.back() = numbers.back() * 10 + (c - '0');
+ } else {
+ in_number = true;
+ numbers.push_back(c - '0');
+ }
+ }
+ std::sort(numbers.begin(), numbers.end());
+ bool first_flag = true;
+ for (int x : numbers) {
+ if (!first_flag) { std::cout << ','; }
+ std::cout << x;
+ first_flag = false;
+ }
+ if (first_flag) {
+ std::cout << 0 << std::endl;
+ }
+
+
+ return 0;
+}
diff --git a/dsa/ch4_string/dsa_0403_within_string.cpp b/dsa/ch4_string/dsa_0403_within_string.cpp
new file mode 100644
index 0000000..d38511a
--- /dev/null
+++ b/dsa/ch4_string/dsa_0403_within_string.cpp
@@ -0,0 +1,20 @@
+#include
+
+bool is_within(const std::string& str, const std::string& substr) {
+ int i = 0, j = 0;
+ for (; i < str.size() && j < substr.size(); i++) {
+ if (str[i] == substr[j]) { j++; }
+ }
+ return j == substr.size();
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::string substr, str;
+ while (std::cin >> substr >> str) {
+ std::cout << (is_within(str, substr) ? "Yes" : "No") << std::endl;
+ }
+
+ return 0;
+}
diff --git a/dsa/ch4_string/dsa_0404_string_loop.cpp b/dsa/ch4_string/dsa_0404_string_loop.cpp
new file mode 100644
index 0000000..181b187
--- /dev/null
+++ b/dsa/ch4_string/dsa_0404_string_loop.cpp
@@ -0,0 +1,47 @@
+#include
+#include
+
+std::ostream& operator<<(std::ostream& os, const std::vector& vector) {
+ for (const auto elem : vector) {
+ os << elem << ' ';
+ }
+ return os;
+}
+
+std::vector prefix_function(const std::string& s) {
+ std::vector pi(s.size());
+ for (int i = 1; i < s.size(); i++) {
+ int j = pi[i - 1];
+ while (true) {
+ if (s[i] == s[j]) {
+ pi[i] = j + 1;
+ break;
+ }
+ if (j == 0) {
+ pi[i] = 0;
+ break;
+ }
+ j = pi[j - 1];
+ }
+ }
+ return pi;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::string s;
+ while (std::cin >> s && s != ".") {
+ auto pi = prefix_function(s);
+ std::clog << pi << std::endl;
+ int period = static_cast(s.size()) - pi.back();
+ if (s.size() % period != 0) {
+ std::cout << 1 << std::endl;
+ } else {
+ std::cout << s.size() / period << std::endl;
+ }
+ }
+
+ return 0;
+}
diff --git a/dsa/ch4_string/dsa_0406_valid_string.cpp b/dsa/ch4_string/dsa_0406_valid_string.cpp
new file mode 100644
index 0000000..0f5c0de
--- /dev/null
+++ b/dsa/ch4_string/dsa_0406_valid_string.cpp
@@ -0,0 +1,58 @@
+#include
+#include
+
+bool matches(std::string s, std::string pattern) {
+ int i = 0, j = 0;
+ bool within_bracket = false;
+ while (true) {
+ if (!within_bracket) {
+ if (pattern[j] == '[') {
+ within_bracket = true;
+ j++;
+ continue;
+ }
+ if (s[i] != pattern[j]) { return false; }
+ i++, j++;
+ } else {
+ bool matched = false;
+ while (pattern[j] != ']') {
+ matched = s[i] == pattern[j] || matched;
+ within_bracket = false;
+ j++;
+ }
+ if (!matched) { return false; }
+ i++, j++;
+ }
+ if (i == s.size() && j == pattern.size()) { return true; }
+ if (i == s.size() || j == pattern.size()) { return false; }
+ }
+ throw std::logic_error("What the fuck is this piece of WCJ doing?");
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ int n;
+ std::string pattern;
+ std::cin >> n;
+ std::vector strs(n);
+ for (int i = 0; i < n; i++) {
+ std::cin >> strs[i];
+ }
+ std::cin >> pattern;
+ for (char& c : pattern) {
+ if (std::isupper(c)) { c -= 'A' - 'a'; }
+ }
+
+ for (int i = 0; i < n; i++) {
+ std::string s = strs[i];
+ for (char& c : s) {
+ if (std::isupper(c)) { c -= 'A' - 'a'; }
+ }
+ if (matches(s, pattern)) {
+ std::cout << i + 1 << ' ' << strs[i] << std::endl;
+ }
+ }
+
+ return 0;
+}
diff --git a/dsa/ch4_string/luogu_template_kmp_P3375.cpp b/dsa/ch4_string/luogu_template_kmp_P3375.cpp
new file mode 100644
index 0000000..4c9e2ca
--- /dev/null
+++ b/dsa/ch4_string/luogu_template_kmp_P3375.cpp
@@ -0,0 +1,49 @@
+#include
+#include
+
+std::vector prefix_function(const std::string& s) {
+ std::vector pi(s.size());
+ for (int i = 1; i < s.size(); i++) {
+ int j = pi[i - 1];
+ while (j > 0 && s[i] != s[j]) {
+ j = pi[j - 1];
+ }
+ if (s[i] == s[j]) { j++; }
+ pi[i] = j;
+ }
+ return pi;
+}
+
+std::vector find_substring_pos(const std::string& target, const std::string& pattern) {
+ std::vector matched_pos;
+ const std::vector pattern_prefix_values = prefix_function(pattern);
+ int j = 0;
+ for (int i = 0; i < target.size(); i++) {
+ while (j > 0 && target[i] != pattern[j]) {
+ j = pattern_prefix_values[j - 1];
+ }
+ if (target[i] == pattern[j]) { j++; }
+ if (j == pattern.size()) {
+ matched_pos.push_back(i - static_cast(pattern.size()) + 1);
+ j = pattern_prefix_values[j - 1];
+ }
+ }
+ return matched_pos;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::string target, pattern;
+ std::cin >> target >> pattern;
+ for (const auto i : find_substring_pos(target, pattern)) {
+ std::cout << i + 1 << std::endl;
+ }
+ for (const auto val : prefix_function(pattern)) {
+ std::cout << val << " ";
+ }
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/dsa/ch5_binary_tree/dsa_0510_reconstruct_tree.cpp b/dsa/ch5_binary_tree/dsa_0510_reconstruct_tree.cpp
new file mode 100644
index 0000000..08f55d6
--- /dev/null
+++ b/dsa/ch5_binary_tree/dsa_0510_reconstruct_tree.cpp
@@ -0,0 +1,106 @@
+#include
+#include
+#include
+#include
+#include
+
+class BinaryTree {
+public:
+ struct Node {
+ int value = -1;
+ Node *father = nullptr, *left = nullptr, *right = nullptr;
+ };
+
+ Node* root = nullptr;
+
+ void reconstruct(const std::vector& inorder_traversal, const std::vector& postorder_traversal) {
+ assert(inorder_traversal.size() == postorder_traversal.size());
+ root = reconstruct_node(
+ inorder_traversal, postorder_traversal,
+ 0, inorder_traversal.size(),
+ 0, inorder_traversal.size());
+ }
+
+ [[nodiscard]] std::vector preorder_traversal() {
+ std::vector result;
+ preorder_dfs(root, result);
+ return result;
+ }
+
+protected:
+ Node* reconstruct_node(
+ const std::vector& inorder_traversal, const std::vector& postorder_traversal,
+ std::size_t inorder_begin, std::size_t inorder_end, std::size_t postorder_begin, std::size_t postorder_end
+ ) {
+ if (inorder_end - inorder_begin == 1) {
+ return new Node({.value = inorder_traversal[inorder_begin]});
+ }
+ if (inorder_end == inorder_begin) { return nullptr; }
+ auto in_begin = inorder_traversal.begin() + inorder_begin;
+ auto in_end = inorder_traversal.begin() + inorder_end;
+ auto root_idx = std::find(
+ in_begin, in_end,
+ postorder_traversal[postorder_end - 1]
+ ) - inorder_traversal.begin();
+ auto left_length = root_idx - inorder_begin;
+ auto right_length = inorder_end - root_idx - 1;
+
+ Node* left_node = reconstruct_node(
+ inorder_traversal, postorder_traversal,
+ inorder_begin, root_idx, postorder_begin, postorder_begin + left_length
+ );
+ Node* right_node = reconstruct_node(
+ inorder_traversal, postorder_traversal,
+ root_idx + 1, inorder_end, postorder_end - 1 - right_length, postorder_end - 1
+ );
+
+ Node* node = new Node({
+ .value = inorder_traversal[root_idx],
+ .left = left_node, .right = right_node,
+ });
+ if (left_node != nullptr) { left_node->father = node; }
+ if (right_node != nullptr) { right_node->father = node; }
+ return node;
+ }
+
+ void preorder_dfs(Node* node, std::vector& result) {
+ if (node == nullptr) { return; }
+ result.push_back(node->value);
+ preorder_dfs(node->left, result);
+ preorder_dfs(node->right, result);
+ }
+};
+
+
+std::ostream& operator<<(std::ostream& os, const std::vector& vector) {
+ for (int x : vector) { os << x << ' '; }
+ return os;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::vector inorder_traversal, postorder_traversal;
+
+ std::string lines[2];
+ std::getline(std::cin, lines[0]);
+ std::getline(std::cin, lines[1]);
+
+ std::istringstream iss1(lines[0]), iss2(lines[1]);
+ int temp;
+ while (iss1 >> temp) {
+ inorder_traversal.push_back(temp);
+ }
+ while (iss2 >> temp) {
+ postorder_traversal.push_back(temp);
+ }
+
+ std::clog << inorder_traversal << std::endl << postorder_traversal << std::endl;
+
+ BinaryTree binary_tree;
+ binary_tree.reconstruct(inorder_traversal, postorder_traversal);
+ std::cout << binary_tree.preorder_traversal() << std::endl;
+
+ return 0;
+}
diff --git a/dsa/ch5_binary_tree/luogu_P4715_matches.cpp b/dsa/ch5_binary_tree/luogu_P4715_matches.cpp
new file mode 100644
index 0000000..6d99dab
--- /dev/null
+++ b/dsa/ch5_binary_tree/luogu_P4715_matches.cpp
@@ -0,0 +1,62 @@
+#include
+#include
+#include
+
+class BinaryTree {
+public:
+ struct Node {
+ int value = -1;
+ Node *father = nullptr, *left = nullptr, *right = nullptr;
+ int depth = -1;
+ };
+
+ Node* root = new Node({.depth = 0});
+
+ void init_full_tree(int height, const std::function& comp) {
+ int id = 0;
+ return init_full_tree(height, comp, 0, nullptr, id);
+ }
+
+protected:
+ void init_full_tree(int height, const std::function& comp,
+ int current_depth, Node* current_node, int& id) {
+ if (current_depth == 0) { current_node = root; }
+ if (current_depth == height) {
+ current_node->value = id++;
+ return;
+ }
+ current_node->left = new Node();
+ current_node->right = new Node();
+ init_full_tree(height, comp, current_depth + 1, current_node->left, id);
+ init_full_tree(height, comp, current_depth + 1, current_node->right, id);
+ if (comp(current_node->left->value, current_node->right->value)) {
+ current_node->value = current_node->right->value;
+ } else {
+ current_node->value = current_node->left->value;
+ }
+ }
+};
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ BinaryTree binary_tree;
+ std::vector strengths;
+ std::cin >> n;
+ strengths.resize(static_cast(std::pow(2, n)));
+ for (int& strength : strengths) { std::cin >> strength; }
+ binary_tree.init_full_tree(n,
+ [strengths](int i, int j) {
+ return strengths[i] < strengths[j];
+ });
+ // std::cout << binary_tree.root->value << std::endl;
+ int gold = binary_tree.root->value;
+ int silver = binary_tree.root->left->value == gold
+ ? binary_tree.root->right->value
+ : binary_tree.root->left->value;
+ std::cout << silver + 1 << std::endl;
+
+ return 0;
+}
diff --git a/dsa/ch5_binary_tree/luogu_T750119_bin_tree_iter.cpp b/dsa/ch5_binary_tree/luogu_T750119_bin_tree_iter.cpp
new file mode 100644
index 0000000..8fc4bbf
--- /dev/null
+++ b/dsa/ch5_binary_tree/luogu_T750119_bin_tree_iter.cpp
@@ -0,0 +1,103 @@
+#include
+#include
+
+constexpr int MAX_N = 1e5 + 1;
+
+class BinaryTree {
+public:
+ using NodeIdx = int;
+
+ struct Node {
+ NodeIdx father = -1, left = -1, right = -1;
+ };
+
+ NodeIdx root = -1;
+ std::vector nodes;
+
+ explicit BinaryTree(int n) : nodes(n) {}
+
+ NodeIdx init_root() {
+ root = 0;
+ while (nodes[root].father != -1) {
+ root = nodes[root].father;
+ }
+ return root;
+ }
+
+ [[nodiscard]] std::vector preorder_traversal() {
+ std::vector result;
+ result.reserve(nodes.size());
+ preorder_dfs(root, result);
+ return result;
+ }
+
+ [[nodiscard]] std::vector postorder_transversal() {
+ std::vector result;
+ result.reserve(nodes.size());
+ postorder_dfs(root, result);
+ return result;
+ }
+
+ [[nodiscard]] std::vector inorder_transversal() {
+ std::vector result;
+ result.reserve(nodes.size());
+ inorder_dfs(root, result);
+ return result;
+ }
+
+protected:
+ void preorder_dfs(NodeIdx node, std::vector& result) {
+ if (node == -1) { return; }
+ result.push_back(node);
+ preorder_dfs(nodes[node].left, result);
+ preorder_dfs(nodes[node].right, result);
+ }
+
+ void postorder_dfs(NodeIdx node, std::vector& result) {
+ if (node == -1) { return; }
+ postorder_dfs(nodes[node].left, result);
+ postorder_dfs(nodes[node].right, result);
+ result.push_back(node);
+ }
+
+ void inorder_dfs(NodeIdx node, std::vector& result) {
+ if (node == -1) { return; }
+ inorder_dfs(nodes[node].left, result);
+ result.push_back(node);
+ inorder_dfs(nodes[node].right, result);
+ }
+};
+
+std::ostream& operator<<(std::ostream& os, const std::vector& vector) {
+ for (int x : vector) { os << x + 1 << ' '; }
+ return os;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+
+ BinaryTree binary_tree(n);
+
+ for (int i = 0; i < n; i++) {
+ int x, y;
+ std::cin >> x >> y;
+ x--, y--;
+
+ binary_tree.nodes[i].left = x;
+ binary_tree.nodes[i].right = y;
+ if (x >= 0) { binary_tree.nodes[x].father = i; }
+ if (y >= 0) { binary_tree.nodes[y].father = i; }
+ }
+
+ binary_tree.init_root();
+
+ std::cout << binary_tree.preorder_traversal() << std::endl;
+ std::cout << binary_tree.inorder_transversal() << std::endl;
+ std::cout << binary_tree.postorder_transversal() << std::endl;
+
+ return 0;
+}