From 2490f8091a136f71eecaf01bd1336faef2a9251c Mon Sep 17 00:00:00 2001 From: arielherself Date: Sat, 5 Oct 2024 19:28:38 +0800 Subject: [PATCH] regular backup --- home-manager/home.nix | 4 ++ nvim/lua/snippets/hierholzer.lua | 66 ++++++++++++++++++++++ nvim/lua/snippets/init.lua | 27 +++++++++ nvim/lua/snippets/quick-union-rollback.lua | 46 +++++++++++++++ nvim/lua/snippets/quick-union.lua | 4 +- 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 nvim/lua/snippets/hierholzer.lua create mode 100644 nvim/lua/snippets/quick-union-rollback.lua diff --git a/home-manager/home.nix b/home-manager/home.nix index 5d2b888..300bb6d 100644 --- a/home-manager/home.nix +++ b/home-manager/home.nix @@ -174,6 +174,9 @@ in { pkgs.nix-index pkgs.rclone pkgs.sshfs + pkgs.usbutils + pkgs.udiskie + pkgs.udisks # Pwn pkgs.nmap @@ -364,6 +367,7 @@ in { # This plugin seems to break terminfo when default shell of a terminal emulator is set to zsh. # set -g @plugin 'tmux-plugins/tmux-sensible' set -g @plugin 'erikw/tmux-powerline' + set -g @plugin 'tmux-plugins/tmux-yank' run '${config.xdg.configHome}/tmux/plugins/tpm/tpm' ''; }; diff --git a/nvim/lua/snippets/hierholzer.lua b/nvim/lua/snippets/hierholzer.lua new file mode 100644 index 0000000..cf766f7 --- /dev/null +++ b/nvim/lua/snippets/hierholzer.lua @@ -0,0 +1,66 @@ +return [=[ +struct hierholzer { + struct edge_t { + int from, to, rev, stale; + edge_t(int from, int to, int rev, int stale) : from(from), to(to), rev(rev), stale(stale) {} + }; + int n; + vector edges; + vector> ch; + quick_union qu; + + hierholzer(int n) : n(n), ch(n + 1), qu(n + 1) {} + + void add_edge(int u, int v) { + int left = edges.size(), right = left + 1; + edges.emplace_back(u, v, right, false); + edges.emplace_back(v, u, left, false); + ch[u].emplace_back(left); + ch[v].emplace_back(right); + qu.merge(u, v); + } + + optional> solve() { + if (qu.query_size(1) != n) { + return nullopt; + } + + int odd = 0; + int last_odd; + for (int i = 1; i <= n; ++i) { + if (ch[i].size() % 2) { + odd += 1; + last_odd = i; + } + } + + int start; + if (odd == 0) { + start = 1; + } else if (odd == 2) { + start = last_odd; + } else { + return nullopt; + } + + vector res; + + auto dfs = [&] (auto&& dfs, int v) -> void { + while (ch[v].size()) { + int i = ch[v].back(); + ch[v].pop_back(); + if (not edges[i].stale) { + edges[i].stale = true; + edges[edges[i].rev].stale = true; + dfs(dfs, edges[i].to); + } + } + res.emplace_back(v); + }; + dfs(dfs, start); + + return res; + } +}; + +]=] diff --git a/nvim/lua/snippets/init.lua b/nvim/lua/snippets/init.lua index 19031c1..ba30509 100644 --- a/nvim/lua/snippets/init.lua +++ b/nvim/lua/snippets/init.lua @@ -76,6 +76,19 @@ ls.add_snippets(nil, { } }) +local quick_union_rollback = require('snippets.quick-union-rollback') +ls.add_snippets(nil, { + cpp = { + snip({ + trig = 'quick_union_rollback', + namr = 'quick_union_rollback', + dscr = 'Union Find (with Rollback)', + },{ + text(lines(quick_union_rollback)) + }) + } +}) + local sparse_table = require('snippets.sparse-table') ls.add_snippets(nil, { cpp = { @@ -339,3 +352,17 @@ ls.add_snippets(nil, { }) } }) + +local hierholzer = require('snippets.hierholzer') +ls.add_snippets(nil, { + cpp = { + snip({ + trig = 'hierholzer', + namr = 'hierholzer', + dscr = 'Find Euler path', + },{ + text(lines(hierholzer)) + }) + } +}) + diff --git a/nvim/lua/snippets/quick-union-rollback.lua b/nvim/lua/snippets/quick-union-rollback.lua new file mode 100644 index 0000000..59afe03 --- /dev/null +++ b/nvim/lua/snippets/quick-union-rollback.lua @@ -0,0 +1,46 @@ +return [=[ +class quick_union { +public: + size_t n; + vector c, sz; + vector> history; +public: + quick_union(size_t n) : n(n), c(n), sz(n) { + iota(c.begin(), c.end(), 0); + sz.assign(n, 1); + } + + size_t query(size_t i) { + if (c[i] != i) return query(c[i]); + return c[i]; + } + + void merge(size_t i, size_t j) { + if (connected(i, j)) { + history.emplace_back(n, n); + } else { + i = query(i), j = query(j); + if (sz[i] > sz[j]) swap(i, j); + history.emplace_back(i, j); + sz[j] += sz[i]; + c[i] = j; + } + } + + bool connected(size_t i, size_t j) { + return query(i) == query(j); + } + + size_t query_size(size_t i) { + return sz[query(i)]; + } + + void rollback() { + auto [i, j] = popback(history); + if (i == n and j == n) return; + c[i] = i; + sz[j] -= sz[i]; + } +}; + +]=] diff --git a/nvim/lua/snippets/quick-union.lua b/nvim/lua/snippets/quick-union.lua index d145be1..7f2246f 100644 --- a/nvim/lua/snippets/quick-union.lua +++ b/nvim/lua/snippets/quick-union.lua @@ -7,12 +7,12 @@ public: iota(c.begin(), c.end(), 0); sz.assign(n, 1); } - + size_t query(size_t i) { if (c[i] != i) c[i] = query(c[i]); return c[i]; } - + void merge(size_t i, size_t j) { if (connected(i, j)) return; sz[query(j)] += sz[query(i)];