diff --git a/.idea/editor.xml b/.idea/editor.xml
index f86872e..14cdacd 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -255,7 +255,6 @@
-
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1108cee..e596ce6 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -21,15 +21,16 @@
+
-
+
@@ -39,13 +40,16 @@
+
+
+
@@ -60,10 +64,13 @@
+
+
+
@@ -75,30 +82,21 @@
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
@@ -154,93 +152,97 @@
- {
+ "keyToString": {
+ "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
+ "C/C++ 文件.D_pojcs101_M02692.cpp.executor": "Run",
+ "C/C++ 文件.luogu_P3916_graph_iteration.cpp.executor": "Debug",
+ "C/C++ 文件.luogu_UVA755.cpp.executor": "Run",
+ "C/C++ 文件.luogu_ez_B3628.cpp.executor": "Run",
+ "C/C++ 文件.luogu_model_dsu_P3367.cpp.executor": "Run",
+ "C/C++ 文件.noip2003j_bigint.cpp.executor": "Run",
+ "C/C++ 文件.ojcs101_E01003.cpp.executor": "Run",
+ "C/C++ 文件.ojcs101_E01218.cpp.executor": "Run",
+ "C/C++ 文件.ojcs101_E02883.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M01017_6_boxes.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M01088_ski.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M01321_chest_grid.cpp.executor": "Debug",
+ "C/C++ 文件.pojcs101_M01328_radar_greedy.cpp.executor": "Debug",
+ "C/C++ 文件.pojcs101_M01852_ants_falling.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M01922_ride_follower.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M02039_snakey_encryption.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M02431_cows_gas_leak.cpp.executor": "Debug",
+ "C/C++ 文件.pojcs101_M02524_ubiquitous_religions_dsu.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M02692.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M02692_12_coins.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M02694_polish_notation_tree.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M03247_palindromic_prime.cpp.executor": "Run",
+ "C/C++ 文件.pojcs101_M03441_sum_zero.cpp.executor": "Run",
+ "CMake 应用程序.JLOI2009_binary_tree.executor": "Run",
+ "CMake 应用程序.NOIP2005J_herbs_dp_backpack.executor": "Run",
+ "CMake 应用程序.NOIP2005J_herbs_dp_backpack_MWASTE_BETTER.executor": "Run",
+ "CMake 应用程序.NOIP2006S_jinming_budget_dp_knapsack.executor": "Run",
+ "CMake 应用程序.NOIP2016J_bay.executor": "Run",
+ "CMake 应用程序.SHOI2002_ski_dp.executor": "Run",
+ "CMake 应用程序.dp__TJOI2007_walk_segments_dp.executor": "Run",
+ "CMake 应用程序.dsa__linear_list__dsa_0201_dictionary.executor": "Run",
+ "CMake 应用程序.exercises.executor": "Run",
+ "CMake 应用程序.gdbh.executor": "Run",
+ "CMake 应用程序.graph__luogu_P3916_graph_iteration.executor": "Run",
+ "CMake 应用程序.lc_135_candy.executor": "Run",
+ "CMake 应用程序.luogu_P1160_biodir_linked_list.executor": "Run",
+ "CMake 应用程序.luogu_P2196_miner_dp.executor": "Run",
+ "CMake 应用程序.noip2003j_bigint.executor": "Run",
+ "CMake 应用程序.pojcs101_M02694_polish_notation_tree.executor": "Run",
+ "CMake 应用程序.pojcs101_M03441_sum_zero.executor": "Run",
+ "CMake 应用程序.pojcs101_M03532.executor": "Run",
+ "CMake 应用程序.pojcs101_M03704_brackets.executor": "Run",
+ "CMake 应用程序.pojcs101_T01011_sticks.executor": "Run",
+ "CMake 应用程序.pojcs101_T04117_split_int.executor": "Run",
+ "CMake 应用程序.pojcs101_T12559_biggest_smallest_combination.executor": "Run",
+ "CMake 应用程序.pojcs101_T27104_chicken_monitors.executor": "Run",
+ "CMake 应用程序.pojcs101_T27373_biggest_combination_t2.executor": "Run",
+ "CMake 应用程序.test.executor": "Run",
+ "CMake 调试.exercises.executor": "Debug",
+ "CidrDebugProfileConversion": "true",
+ "ModuleVcsDetector.initialDetectionPerformed": "true",
+ "Python.lc_127.executor": "Run",
+ "Python.noi2001_food_chain_dsu.executor": "Run",
+ "Python.pojcs101_M03247_palindromic_prime_init1.executor": "Debug",
+ "RunOnceActivity.RadMigrateCodeStyle": "true",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "RunOnceActivity.cidr.known.project.marker": "true",
+ "RunOnceActivity.git.unshallow": "true",
+ "RunOnceActivity.readMode.enableVisualFormatting": "true",
+ "RunOnceActivity.typescript.service.memoryLimit.init": "true",
+ "RunOnceActivity.west.config.association.type.startup.service": "true",
+ "cf.first.check.clang-format": "false",
+ "cidr.known.project.marker": "true",
+ "codeWithMe.voiceChat.enabledByDefault": "false",
+ "deletionFromPopupRequiresConfirmation": "false",
+ "git-widget-placeholder": "main",
+ "last_opened_file_path": "/Users/roger/coding/exercises/graph",
+ "node.js.detected.package.jshint": "true",
+ "node.js.selected.package.jshint": "",
+ "nodejs_package_manager_path": "npm",
+ "org.rust.first.attach.projects": "true",
+ "settings.editor.selected.configurable": "preferences.sourceCode.C++"
}
-}]]>
+}
-
+
+
+
-
-
-
-
-
-
+
@@ -261,11 +263,26 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -336,11 +353,36 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -351,11 +393,6 @@
-
-
-
-
-
@@ -564,11 +601,18 @@
+
+
+
+
+
+
+
-
+
@@ -605,37 +649,79 @@
-
+
- {
+ "profileIdByStamp": [
{
- "first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序._others__qyzgdbh",
- "cmake": "CMakeBuildProfile:Debug-GCC"
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.JLOI2009_binary_tree",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
},
- "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
{
- "first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.NOIP2003J_bigint",
- "cmake": "CMakeBuildProfile:Debug-GCC"
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序._others__qyz__gdbh",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
},
- "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
{
- "first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.pojcs101_T27373_biggest_combination_t2",
- "cmake": "CMakeBuildProfile:Debug-GCC"
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dp__TJOI2007_walk_segments_dp",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
},
- "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.luogu_P1160_biodir_linked_list",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.NOIP2016J_bay",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.graph__luogu_P3916_graph_iteration",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0201_dictionary",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.NOIP2003J_bigint",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.pojcs101_T27373_biggest_combination_t2",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
}
],
- "modificationCount": 49
-}]]>
+ "modificationCount": 69
+}
@@ -670,7 +756,8 @@
-
+
+
@@ -685,8 +772,28 @@
32
+
+ file://$PROJECT_DIR$/dp/TJOI2007_walk_segments_dp.cpp
+ 35
+
+
+
+ file://$PROJECT_DIR$/dp/TJOI2007_walk_segments_dp.cpp
+ 45
+
+
+
+ file://$PROJECT_DIR$/graph/luogu_P3916_graph_iteration.cpp
+ 66
+
+
+
+
+
+
+
diff --git a/JLOI2009_binary_tree.cpp b/JLOI2009_binary_tree.cpp
new file mode 100644
index 0000000..becdd98
--- /dev/null
+++ b/JLOI2009_binary_tree.cpp
@@ -0,0 +1,87 @@
+#include
+#include
+#include
+
+using namespace std;
+
+class Graph {
+public:
+ struct Node {
+ vector neighbors;
+ int father = -1;
+ int depth = -1;
+ };
+
+ vector nodes;
+ int depth = -1, breadth = -1;
+
+ void connect(int x, int y) {
+ assert(x < nodes.size() && y < nodes.size());
+ nodes[x].neighbors.push_back(y);
+ nodes[y].neighbors.push_back(x);
+ }
+
+ void init_tree() {
+ vector depths_map;
+ init_tree(0, 0, depths_map);
+ depth = depths_map.size();
+ breadth = -1;
+ for (const auto& d : depths_map) {
+ breadth = max(breadth, d);
+ }
+ }
+
+ void init_tree(int node_idx, int current_depth, vector& depths_map) {
+ if (depths_map.size() <= current_depth) { depths_map.push_back(0); }
+ depths_map[current_depth]++;
+ for (const auto& neighbor : nodes[node_idx].neighbors) {
+ if (neighbor == nodes[node_idx].father) { continue; }
+ nodes[neighbor].father = node_idx;
+ nodes[neighbor].depth = current_depth + 1;
+ init_tree(neighbor, current_depth + 1, depths_map);
+ }
+ }
+
+ int get_cost(int start, int end) {
+ int cost = 0;
+ while (nodes[start].depth > nodes[end].depth) {
+ cost += 2;
+ start = nodes[start].father;
+ }
+ while (nodes[start].depth < nodes[end].depth) {
+ cost += 1;
+ end = nodes[end].father;
+ }
+ while (start != end) {
+ cost += 3;
+ start = nodes[start].father;
+ end = nodes[end].father;
+ }
+ return cost;
+ }
+};
+
+
+Graph tree;
+int n;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int x, y;
+
+ cin >> n;
+ tree.nodes.resize(n);
+ for (int _ = 1; _ < n; _++) {
+ cin >> x >> y;
+ tree.connect(x - 1, y - 1);
+ }
+ tree.init_tree();
+ cout << tree.depth << endl << tree.breadth << endl;
+
+ cin >> x >> y;
+ cout << tree.get_cost(x - 1, y - 1) << endl;
+
+ return 0;
+}
diff --git a/JLOI2009_binary_tree_GIVEUP.cpp b/JLOI2009_binary_tree_GIVEUP.cpp
new file mode 100644
index 0000000..cb320ea
--- /dev/null
+++ b/JLOI2009_binary_tree_GIVEUP.cpp
@@ -0,0 +1,64 @@
+#include
+#include
+#include
+
+using namespace std;
+
+class BinaryTree {
+public:
+ struct Node {
+ int left = -1, right = -1, father = -1;
+ };
+
+ vector nodes;
+
+ int depth = -1, breadth = -1;
+
+ void connect(int father, int son) {
+ assert(father < nodes.size() && son < nodes.size());
+ if (nodes[son].father != -1 && nodes[father].father == -1) { swap(father, son); }
+ nodes[son].father = father;
+ if (nodes[father].left == -1) {
+ nodes[father].left = son;
+ } else {
+ nodes[father].right = son;
+ }
+ }
+
+ void dfs() {
+ depth = -1, breadth = -1;
+ vector depths_map;
+ dfs(0, 0, depths_map);
+ for (const int d : depths_map) {
+ breadth = max(breadth, d);
+ }
+ }
+
+ void dfs(int node_idx, int _depth, vector& depths_map) {
+ depth = max(depth, _depth);
+ if (_depth >= depths_map.size()) {
+ depths_map.push_back(0);
+ }
+ depths_map[_depth]++;
+ if (nodes[node_idx].left != -1) {
+ dfs(nodes[node_idx].left, _depth + 1, depths_map);
+ }
+ if (nodes[node_idx].right != -1) {
+ dfs(nodes[node_idx].right, _depth + 1, depths_map);
+ }
+ }
+};
+
+BinaryTree tree;
+int n;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> n;
+ tree.nodes.resize(n);
+
+
+ return 0;
+}
diff --git a/NOIP2016J_bay.cpp b/NOIP2016J_bay.cpp
new file mode 100644
index 0000000..7a2ba9c
--- /dev/null
+++ b/NOIP2016J_bay.cpp
@@ -0,0 +1,71 @@
+/*
+ * 在线处理?
+ */
+
+#include
+#include
+#include
+
+using namespace std;
+
+using Boat = pair>;
+
+constexpr int MAX_N = 1e5 + 5, DAY_TIME = 86400;
+
+vector boats;
+unordered_map nations_statistics;
+int start_time = -DAY_TIME, current_time = 0, pointer = 0, nations_count;
+
+void modify_nations_statistics(vector people, int multiplier = 1) {
+ for (int nation : people) {
+ if (nations_statistics[nation] == 0) { nations_count++; }
+ nations_statistics[nation] += multiplier;
+ if (nations_statistics[nation] == 0) { nations_count--; }
+ }
+}
+
+int count_nations() {
+ // int ans = 0;
+ // for (auto& [nation, count] : nations_statistics) {
+ // if (count > 0) { ans++; }
+ // }
+ // return ans;
+ return nations_count;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int n;
+ cin >> n;
+ for (int i = 0; i < n; i++) {
+ int arrival_time, people_amount;
+ cin >> arrival_time >> people_amount;
+ boats.emplace_back(arrival_time, vector(people_amount));
+ for (int j = 0; j < people_amount; j++) {
+ cin >> boats.back().second[j];
+ }
+ // ONLINE
+ modify_nations_statistics(boats.back().second);
+ start_time = arrival_time - DAY_TIME;
+ while (pointer < boats.size() && boats[pointer].first <= start_time) {
+ modify_nations_statistics(boats[pointer].second, -1);
+ pointer++;
+ }
+ cout << count_nations() << endl;
+ }
+
+ // for (int i = 0, j = 0; i < boats.size(); i++) {
+ // current_time = boats[i].first + DAY_TIME;
+ // // modify_nations_statistics(boats[i].second);
+ // while (j < boats.size() && boats[j].first <= current_time) {
+ // modify_nations_statistics(boats[j].second);
+ // j++;
+ // }
+ // cout << count_nations() << endl;
+ // modify_nations_statistics(boats[i].second, -1);
+ // }
+
+ return 0;
+}
diff --git a/dp/TJOI2007_walk_segments_dp.cpp b/dp/TJOI2007_walk_segments_dp.cpp
new file mode 100644
index 0000000..f1148a0
--- /dev/null
+++ b/dp/TJOI2007_walk_segments_dp.cpp
@@ -0,0 +1,64 @@
+#include
+
+using namespace std;
+
+using Segment = pair;
+
+constexpr int MAX_N = 20005;
+int n;
+Segment segments[MAX_N];
+
+pair dp[MAX_N]; // 从这一层的线段左/右边出去
+
+void log_dp() {
+ for (int i = 0; i < n; i++) {
+ clog << dp[i].first << "\t" << dp[i].second << endl;
+ }
+}
+
+pair get_min_steps(const Segment& segment, const int start_pos) {
+ const int len = segment.second - segment.first;
+ if (start_pos <= segment.first) {
+ return {segment.second - start_pos + len, segment.second - start_pos};
+ }
+ if (start_pos >= segment.second) {
+ return {start_pos - segment.first, start_pos - segment.first + len};
+ }
+ return {
+ len + (segment.second - start_pos),
+ len + (start_pos - segment.first)
+ };
+}
+
+// pair get_min_steps(const Segment& segment, const Segment& previous_segment) {
+// auto [first1, second1] = get_min_steps(segment, previous_segment.second);
+// auto [first2, second2] = get_min_steps(segment, previous_segment.first);
+// return {min(first1, first2), min(second1, second2)};
+// }
+
+int solve_dp() {
+ dp[0] = get_min_steps(segments[0], 1);
+ for (int i = 1; i < n; i++) {
+ auto [first1, second1] = get_min_steps(segments[i], segments[i - 1].first);
+ auto [first2, second2] = get_min_steps(segments[i], segments[i - 1].second);
+ first1 += dp[i - 1].first, second1 += dp[i - 1].first;
+ first2 += dp[i - 1].second, second2 += dp[i - 1].second;
+ dp[i] = {min(first1, first2), min(second1, second2)};
+ }
+ return min(dp[n - 1].first, dp[n - 1].second);
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> n;
+ for (int i = 0; i < n; i++) {
+ cin >> segments[i].first >> segments[i].second;
+ }
+ segments[n] = {n, n}, n++;
+ cout << solve_dp() + n - 2 << endl;
+ log_dp();
+
+ return 0;
+}
diff --git a/dp/backpack_dp/NOIP2006S_jinming_budget_dp_knapsack.cpp b/dp/backpack_dp/NOIP2006S_jinming_budget_dp_knapsack.cpp
index 596118b..e195ae8 100644
--- a/dp/backpack_dp/NOIP2006S_jinming_budget_dp_knapsack.cpp
+++ b/dp/backpack_dp/NOIP2006S_jinming_budget_dp_knapsack.cpp
@@ -50,7 +50,7 @@ vector generate_flat_items(OriginalItem item) {
void solve_dp() {
for (int i = 0; i < n; i++) {
if (items[i].item.value == -1) { continue; }
- for (int w=capacity; w>=1; w--) {
+ for (int w = capacity; w >= 1; w--) {
// dp[w] = dp[w] // 不买
for (const auto& [weight, value] : generate_flat_items(items[i])) {
if (w >= weight) {
diff --git a/dsa/linear_list/dsa_0201_dictionary.cpp b/dsa/linear_list/dsa_0201_dictionary.cpp
new file mode 100644
index 0000000..b2c8219
--- /dev/null
+++ b/dsa/linear_list/dsa_0201_dictionary.cpp
@@ -0,0 +1,83 @@
+#include
+#include
+#include
+
+using namespace std;
+
+vector words;
+
+string deleted_char(const string& word, const int idx) {
+ assert(0 <= idx && idx < word.size());
+ return word.substr(0, idx) + word.substr(idx + 1);
+}
+
+vector query(const string& s) {
+ vector results;
+ for (string word : words) {
+ switch (static_cast(word.size()) - static_cast(s.size())) {
+ case 1: { // 需要删
+ for (int i = 0; i < word.size(); i++) {
+ if (word[i] != s[i]) {
+ if (deleted_char(word, i) == s) {
+ results.push_back(word);
+ break;
+ }
+ }
+ }
+ break;
+ }
+ case -1: {
+ for (int i = 0; i < s.size(); i++) {
+ if (word[i] != s[i]) {
+ if (deleted_char(s, i) == word) {
+ results.push_back(word);
+ break;
+ }
+ }
+ }
+ break;
+ }
+ case 0: {
+ if (word == s) { return vector{word}; }
+ int diff_count = 0;
+ for (int i = 0; i < s.size(); i++) {
+ if (word[i] != s[i]) {
+ diff_count++;
+ }
+ if (diff_count > 1) { break; }
+ }
+ if (diff_count == 1) {
+ results.push_back(word);
+ break;
+ }
+ break;
+ }
+ default: break;
+ }
+ }
+ return results;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ string s;
+ while (cin >> s && s[0] != '#') {
+ words.push_back(s);
+ }
+ while (cin >> s && s[0] != '#') {
+ vector results = query(s);
+ if (!results.empty() && results[0] == s) {
+ cout << s << " is correct" << endl;
+ continue;
+ }
+ cout << s << ": ";
+ for (const string& result : results) {
+ cout << result << " ";
+ }
+ cout << endl;
+ }
+
+ return 0;
+}
diff --git a/noip2012s_kings_game.cpp b/graph/ECNA2001_sort_letters.cpp
similarity index 100%
rename from noip2012s_kings_game.cpp
rename to graph/ECNA2001_sort_letters.cpp
diff --git a/graph/luogu_P3916_graph_iteration.cpp b/graph/luogu_P3916_graph_iteration.cpp
new file mode 100644
index 0000000..6a32c6f
--- /dev/null
+++ b/graph/luogu_P3916_graph_iteration.cpp
@@ -0,0 +1,75 @@
+#include
+#include
+#include
+
+using namespace std;
+
+class Graph {
+public:
+ using VertexId = int;
+ using EdgeId = int;
+
+ struct Edge {
+ VertexId target_vertex = -1;
+ EdgeId next_edge = -1;
+ };
+
+ vector edges;
+ vector first_edges;
+
+ explicit Graph(int vertices_amount, int edges_amount = 0) : first_edges(vertices_amount, -1) {
+ edges.reserve(edges_amount);
+ }
+
+ void add_edge(VertexId source_vertex, VertexId target_vertex) {
+ const EdgeId edge_id = edges.size();
+ edges.push_back({.target_vertex = target_vertex, .next_edge = first_edges[source_vertex]}); // 所以遍历顺序相反!
+ first_edges[source_vertex] = edge_id;
+ }
+};
+
+constexpr int MAX_V = 1e5 + 5;
+
+int v, e;
+int ans_cache[MAX_V];
+
+int dfs(const Graph& graph, int vertex, int start_vertex) {
+ if (ans_cache[vertex] != -1) { return ans_cache[vertex]; }
+ ans_cache[vertex] = start_vertex;
+ for (int edge_id = graph.first_edges[vertex]; edge_id != -1; edge_id = graph.edges[edge_id].next_edge) {
+ dfs(graph, graph.edges[edge_id].target_vertex, start_vertex);
+ }
+ return ans_cache[vertex];
+}
+
+int dfs(const Graph& graph, int vertex) {
+ // 反向建图,从大到小 dfs
+ return dfs(graph, vertex, vertex);
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> v >> e;
+
+ Graph graph(v, e);
+
+ for (int _ = 0; _ < e; _++) {
+ int x, y;
+ cin >> x >> y;
+ graph.add_edge(y - 1, x - 1); // Reversed graph
+ }
+
+ memset(ans_cache, -1, sizeof(ans_cache));
+
+ for (int vertex = v - 1; vertex >= 0; vertex--) {
+ dfs(graph, vertex);
+ }
+ for (int vertex = 0; vertex < v; vertex++) {
+ cout << ans_cache[vertex] + 1 << " ";
+ }
+ cout << endl;
+
+ return 0;
+}
diff --git a/graph/luogu_P3916_graph_iteration_WRONG.cpp b/graph/luogu_P3916_graph_iteration_WRONG.cpp
new file mode 100644
index 0000000..9185417
--- /dev/null
+++ b/graph/luogu_P3916_graph_iteration_WRONG.cpp
@@ -0,0 +1,69 @@
+#include
+#include
+#include
+
+using namespace std;
+
+class Graph {
+public:
+ using VertexId = int;
+ using EdgeId = int;
+
+ struct Edge {
+ VertexId target_vertex = -1;
+ EdgeId next_edge = -1;
+ };
+
+ vector edges;
+ vector first_edges;
+
+ explicit Graph(int vertices_amount, int edges_amount = 0) : first_edges(vertices_amount, -1) {
+ edges.reserve(edges_amount);
+ }
+
+ void add_edge(VertexId source_vertex, VertexId target_vertex) {
+ const EdgeId edge_id = edges.size();
+ edges.push_back({.target_vertex = target_vertex, .next_edge = first_edges[source_vertex]}); // 所以遍历顺序相反!
+ first_edges[source_vertex] = edge_id;
+ }
+};
+
+constexpr int MAX_V = 1e5 + 5;
+
+int v, e;
+int ans_cache[MAX_V];
+
+int dfs(const Graph& graph, int vertex) {
+ // 如果从小到大 dfs,是不需要在此处取 max 的
+ if (ans_cache[vertex] != -1) { return ans_cache[vertex]; }
+ ans_cache[vertex] = vertex;
+ if (graph.first_edges[vertex] == -1) { return ans_cache[vertex]; }
+ for (int edge_id = graph.first_edges[vertex]; edge_id != -1; edge_id = graph.edges[edge_id].next_edge) {
+ ans_cache[vertex] = max(ans_cache[vertex], dfs(graph, graph.edges[edge_id].target_vertex));
+ }
+ return ans_cache[vertex];
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> v >> e;
+
+ Graph graph(v, e);
+
+ for (int _ = 0; _ < e; _++) {
+ int x, y;
+ cin >> x >> y;
+ graph.add_edge(x - 1, y - 1);
+ }
+
+ memset(ans_cache, -1, sizeof(ans_cache));
+
+ for (int vertex = 0; vertex < v; vertex++) {
+ cout << dfs(graph, vertex) + 1 << " ";
+ }
+ cout << endl;
+
+ return 0;
+}
diff --git a/luogu_P1160_biodir_linked_list.cpp b/luogu_P1160_biodir_linked_list.cpp
new file mode 100644
index 0000000..01a8877
--- /dev/null
+++ b/luogu_P1160_biodir_linked_list.cpp
@@ -0,0 +1,85 @@
+#include
+#include
+#include
+
+using namespace std;
+
+class LinkedList {
+public:
+ struct Node {
+ int previous = -1, next = -1;
+ };
+
+ vector nodes;
+
+ int get_adjacent(int idx, bool next) const {
+ if (next) { return nodes[idx].next; }
+ return nodes[idx].previous;
+ }
+
+ void add(int l, int r) {
+ if (l > -1) { nodes[l].next = nodes.size(); }
+ if (r > -1) { nodes[r].previous = nodes.size(); }
+ nodes.push_back({.previous = l, .next = r});
+ }
+
+ void remove(int idx) {
+ if (nodes[idx].previous > -1) {
+ nodes[nodes[idx].previous].next = nodes[idx].next;
+ }
+ if (nodes[idx].next > -1) {
+ nodes[nodes[idx].next].previous = nodes[idx].previous;
+ }
+ nodes[idx] = {-2, -2};
+ }
+
+ void print() {
+ int start_idx = -1;
+ for (int i = 0; i < nodes.size(); i++) {
+ if (nodes[i].previous == -1) {
+ start_idx = i;
+ break;
+ }
+ }
+ for (int i = start_idx; i != -1; i = nodes[i].next) {
+ cout << i + 1 << " ";
+ }
+ cout << endl;
+ }
+};
+
+LinkedList l;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+
+ int n;
+ int to_be_operated;
+ bool is_next;
+
+ cin >> n;
+
+ l.add(-1, -1);
+
+ for (int _ = 0; _ < n - 1; _++) {
+ cin >> to_be_operated >> is_next;
+ to_be_operated--;
+ int adj = l.get_adjacent(to_be_operated, is_next);
+ if (is_next) {
+ l.add(to_be_operated, adj);
+ } else {
+ l.add(adj, to_be_operated);
+ }
+ }
+ cin >> n;
+ for (int _ = 0; _ < n; _++) {
+ cin >> to_be_operated;
+ l.remove(to_be_operated - 1);
+ }
+
+ l.print();
+
+ return 0;
+}