diff --git a/cses/graph-algorithms/.clang-format b/cses/graph-algorithms/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/cses/graph-algorithms/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: Google +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +AllowShortLoopsOnASingleLine: false diff --git a/cses/graph-algorithms/.clangd b/cses/graph-algorithms/.clangd new file mode 100644 index 0000000..3bb51b3 --- /dev/null +++ b/cses/graph-algorithms/.clangd @@ -0,0 +1,49 @@ +CompileFlags: + Add: + -O2 + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wformat=2 + -Wfloat-equal + -Wlogical-op + -Wshift-overflow=2 + -Wnon-virtual-dtor + -Wold-style-cast + -Wcast-qual + -Wuseless-cast + -Wno-sign-promotion + -Wcast-align + -Wunused + -Woverloaded-virtual + -Wconversion + -Wsign-conversion + -Wmisleading-indentation + -Wduplicated-cond + -Wduplicated-branches + -Wlogical-op + -Wnull-dereference + -Wformat=2 + -Wformat-overflow + -Wformat-truncation + -Wdouble-promotion + -Wundef + -DLOCAL + -Wno-unknown-pragmas +-e -std=c++20 +-e -std=c++23 +-e -std=c++23 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++23 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 +-e -std=c++20 diff --git a/cses/graph-algorithms/building-roads.cc b/cses/graph-algorithms/building-roads.cc new file mode 100644 index 0000000..8502e9d --- /dev/null +++ b/cses/graph-algorithms/building-roads.cc @@ -0,0 +1,130 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +bitset<2 * 100000 + 1> seen; + +void solve() { + seen.reset(); + + u32 n, m; + cin >> n >> m; + + vec> graph(n + 1); + + for (u32 i = 0; i < m; ++i) { + u32 u, v; + cin >> u >> v; + graph[u].push_back(v); + graph[v].push_back(u); + } + + auto dfs = [&](auto&& self, u32 u) { + if (seen[u]) { + return; + } + + seen[u] = true; + + for (auto v : graph[u]) { + self(self, v); + } + }; + + vec component_roots; + for (u32 u = 1; u <= n; ++u) { + if (!seen[u]) { + dfs(dfs, u); + component_roots.push_back(u); + } + } + + cout << component_roots.size() - 1 << '\n'; + for (i32 i = 0; i < component_roots.size() - 1; ++i) { + cout << component_roots[i] << ' ' << component_roots[i + 1] << '\n'; + } +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/building-teams.cc b/cses/graph-algorithms/building-teams.cc new file mode 100644 index 0000000..c467d3d --- /dev/null +++ b/cses/graph-algorithms/building-teams.cc @@ -0,0 +1,137 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + // -1/0/1 -> unset, c0, c1 + vector color(n + 1, -1); + + vec> forest(n + 1); + + for (u32 i = 0; i < m; ++i) { + u32 u, v; + cin >> u >> v; + forest[u].push_back(v); + forest[v].push_back(u); + } + + auto bfs = [&](u32 u) -> bool { + queue q{{u}}; + color[u] = 0; + + while (!q.empty()) { + u = q.front(); + q.pop(); + + for (auto v : forest[u]) { + if (color[v] == color[u]) { + return false; + } + if (color[v] == -1) { + color[v] = 1 - color[u]; + q.push(v); + } + } + } + + return true; + }; + + for (u32 u = 1; u <= n; ++u) { + if (color[u] == -1 && !bfs(u)) { + cout << "IMPOSSIBLE\n"; + return; + } + } + + for (u32 i = 1; i <= n; ++i) { + cout << color[i] + 1 << " \n"[i == n]; + } +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/compile_flags.txt b/cses/graph-algorithms/compile_flags.txt new file mode 100644 index 0000000..cabab0b --- /dev/null +++ b/cses/graph-algorithms/compile_flags.txt @@ -0,0 +1,31 @@ +-pedantic-errors +-O2 +-Wall +-Wextra +-Wpedantic +-Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL +-std=c++20 diff --git a/cses/graph-algorithms/counting-rooms.cc b/cses/graph-algorithms/counting-rooms.cc new file mode 100644 index 0000000..8b6e24d --- /dev/null +++ b/cses/graph-algorithms/counting-rooms.cc @@ -0,0 +1,122 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + vec graph(n); + for (auto& e : graph) + cin >> e; + + auto dfs = [&](auto&& self, int i, int j) -> void { + if (min(i, j) < 0 || i >= n || j >= m || graph[i][j] == '#') { + return; + } + + graph[i][j] = '#'; + + self(self, i + 1, j); + self(self, i - 1, j); + self(self, i, j + 1); + self(self, i, j - 1); + }; + + u32 ans = 0; + + for (u32 i = 0; i < n; ++i) { + for (u32 j = 0; j < m; ++j) { + if (graph[i][j] == '.') { + dfs(dfs, i, j); + ++ans; + } + } + } + + cout << ans << '\n'; +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/debug_flags.txt b/cses/graph-algorithms/debug_flags.txt new file mode 100644 index 0000000..62a0135 --- /dev/null +++ b/cses/graph-algorithms/debug_flags.txt @@ -0,0 +1,14 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC +-DLOCAL +-std=c++23 diff --git a/cses/graph-algorithms/io/building-roads.in b/cses/graph-algorithms/io/building-roads.in new file mode 100644 index 0000000..634438f --- /dev/null +++ b/cses/graph-algorithms/io/building-roads.in @@ -0,0 +1,3 @@ +4 2 +3 1 +2 4 diff --git a/cses/graph-algorithms/io/building-roads.out b/cses/graph-algorithms/io/building-roads.out new file mode 100644 index 0000000..36c83bd --- /dev/null +++ b/cses/graph-algorithms/io/building-roads.out @@ -0,0 +1,6 @@ +1 +1 2 + +[code]: 0 +[time]: 3.44825 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/building-teams.in b/cses/graph-algorithms/io/building-teams.in new file mode 100644 index 0000000..7f18e9a --- /dev/null +++ b/cses/graph-algorithms/io/building-teams.in @@ -0,0 +1,4 @@ +5 3 +1 2 +1 3 +4 5 diff --git a/cses/graph-algorithms/io/building-teams.out b/cses/graph-algorithms/io/building-teams.out new file mode 100644 index 0000000..aabfd7c --- /dev/null +++ b/cses/graph-algorithms/io/building-teams.out @@ -0,0 +1,5 @@ +1 2 2 1 2 + +[code]: 0 +[time]: 2.27332 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/counting-rooms.in b/cses/graph-algorithms/io/counting-rooms.in new file mode 100644 index 0000000..5fd03ea --- /dev/null +++ b/cses/graph-algorithms/io/counting-rooms.in @@ -0,0 +1,6 @@ +5 8 +######## +#..#...# +####.#.# +#..#...# +######## diff --git a/cses/graph-algorithms/io/counting-rooms.out b/cses/graph-algorithms/io/counting-rooms.out new file mode 100644 index 0000000..534365c --- /dev/null +++ b/cses/graph-algorithms/io/counting-rooms.out @@ -0,0 +1,5 @@ +3 + +[code]: 0 +[time]: 4.39477 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/labyrinth.in b/cses/graph-algorithms/io/labyrinth.in new file mode 100644 index 0000000..205e60a --- /dev/null +++ b/cses/graph-algorithms/io/labyrinth.in @@ -0,0 +1,6 @@ +5 8 +######## +#.A#...# +#.##.#B# +#......# +######## diff --git a/cses/graph-algorithms/io/labyrinth.out b/cses/graph-algorithms/io/labyrinth.out new file mode 100644 index 0000000..f45ca3c --- /dev/null +++ b/cses/graph-algorithms/io/labyrinth.out @@ -0,0 +1,7 @@ +YES +9 +LDDRRRRRU + +[code]: 0 +[time]: 3.1817 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/message-route.in b/cses/graph-algorithms/io/message-route.in new file mode 100644 index 0000000..a6d99b2 --- /dev/null +++ b/cses/graph-algorithms/io/message-route.in @@ -0,0 +1,6 @@ +5 5 +1 2 +1 3 +1 4 +2 3 +5 4 diff --git a/cses/graph-algorithms/io/message-route.out b/cses/graph-algorithms/io/message-route.out new file mode 100644 index 0000000..a9f85bf --- /dev/null +++ b/cses/graph-algorithms/io/message-route.out @@ -0,0 +1,6 @@ +3 +1 4 5 + +[code]: 0 +[time]: 2.26545 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/monsters.in b/cses/graph-algorithms/io/monsters.in new file mode 100644 index 0000000..d79f211 --- /dev/null +++ b/cses/graph-algorithms/io/monsters.in @@ -0,0 +1,15 @@ +14 7 +####### +#.....# +#.###.# +#.###.# +#.....# +#A###.. +#####.# +#####.# +#####.# +#####.# +#####.# +#####.# +#####.# +#M....# diff --git a/cses/graph-algorithms/io/monsters.out b/cses/graph-algorithms/io/monsters.out new file mode 100644 index 0000000..b8d07ed --- /dev/null +++ b/cses/graph-algorithms/io/monsters.out @@ -0,0 +1,6 @@ +YES +7 +URRRRDR +[code]: 0 +[time]: 2.22921 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/round-trip.in b/cses/graph-algorithms/io/round-trip.in new file mode 100644 index 0000000..cd1d0ca --- /dev/null +++ b/cses/graph-algorithms/io/round-trip.in @@ -0,0 +1,7 @@ +5 6 +1 3 +1 2 +5 3 +1 5 +2 4 +4 5 diff --git a/cses/graph-algorithms/io/round-trip.out b/cses/graph-algorithms/io/round-trip.out new file mode 100644 index 0000000..70b283c --- /dev/null +++ b/cses/graph-algorithms/io/round-trip.out @@ -0,0 +1,5 @@ +4 +1 5 3 1 +[code]: 0 +[time]: 2.29764 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/shortest-routes-i.in b/cses/graph-algorithms/io/shortest-routes-i.in new file mode 100644 index 0000000..df4d0e4 --- /dev/null +++ b/cses/graph-algorithms/io/shortest-routes-i.in @@ -0,0 +1,6 @@ +3 4 +1 2 6 +1 3 2 +3 2 3 +1 3 4 + diff --git a/cses/graph-algorithms/io/shortest-routes-i.out b/cses/graph-algorithms/io/shortest-routes-i.out new file mode 100644 index 0000000..2e901d0 --- /dev/null +++ b/cses/graph-algorithms/io/shortest-routes-i.out @@ -0,0 +1,5 @@ +0 5 2 + +[code]: 0 +[time]: 2.31624 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/io/shortest-routes-ii.in b/cses/graph-algorithms/io/shortest-routes-ii.in new file mode 100644 index 0000000..90201d4 --- /dev/null +++ b/cses/graph-algorithms/io/shortest-routes-ii.in @@ -0,0 +1,9 @@ +4 3 5 +1 2 5 +1 3 9 +2 3 3 +1 2 +2 1 +1 3 +1 4 +3 2 diff --git a/cses/graph-algorithms/io/shortest-routes-ii.out b/cses/graph-algorithms/io/shortest-routes-ii.out new file mode 100644 index 0000000..e97ec4c --- /dev/null +++ b/cses/graph-algorithms/io/shortest-routes-ii.out @@ -0,0 +1,5 @@ +5 5 8 -1 3 + +[code]: 0 +[time]: 2.57158 ms +[debug]: false \ No newline at end of file diff --git a/cses/graph-algorithms/labyrinth.cc b/cses/graph-algorithms/labyrinth.cc new file mode 100644 index 0000000..211b39d --- /dev/null +++ b/cses/graph-algorithms/labyrinth.cc @@ -0,0 +1,157 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +unordered_map> dirs{ + {'L', {0, -1}}, {'R', {0, 1}}, {'U', {-1, 0}}, {'D', {1, 0}}}; + +void solve() { + u32 n, m; + cin >> n >> m; + + vec grid(n); + for (auto& e : grid) + cin >> e; + + i32 sr, sc, er, ec; + for (u32 r = 0; r < n; ++r) { + for (u32 c = 0; c < m; ++c) { + if (grid[r][c] == 'A') { + sr = r; + sc = c; + } else if (grid[r][c] == 'B') { + er = r; + ec = c; + } + } + } + + queue> q{{{sr, sc}}}; + vec trace(n, string(m, ' ')); + + auto print_trace = [&]() mutable -> void { + string ans; + i32 row = er, col = ec; + + while (grid[row][col] != 'A') { + char step = trace[row][col]; + ans.push_back(step); + row -= dirs.at(step).first; + col -= dirs.at(step).second; + } + + reverse(all(ans)); + cout << ans.size() << '\n' << ans << '\n'; + }; + + while (!q.empty()) { + auto [r, c] = q.front(); + q.pop(); + + if (r == er && c == ec) { + cout << "YES\n"; + print_trace(); + return; + } + + for (auto& [letter, dir] : dirs) { + auto& [dr, dc] = dir; + i32 nr = r + dr; + i32 nc = c + dc; + + if (min(nr, nc) < 0 || nr >= n || nc >= m || grid[nr][nc] == '#' || + trace[nr][nc] != ' ') { + continue; + } + + trace[nr][nc] = letter; + q.emplace(nr, nc); + } + } + + cout << "NO\n"; +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/makefile b/cses/graph-algorithms/makefile new file mode 100644 index 0000000..3c50267 --- /dev/null +++ b/cses/graph-algorithms/makefile @@ -0,0 +1,30 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/cses/graph-algorithms/message-route.cc b/cses/graph-algorithms/message-route.cc new file mode 100644 index 0000000..eb7c642 --- /dev/null +++ b/cses/graph-algorithms/message-route.cc @@ -0,0 +1,139 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + vec> tree(n + 1); + vec par(n + 1, -1); + par[1] = 1; + + u32 u, v; + for (u32 i = 0; i < m; ++i) { + cin >> u >> v; + + tree[u].push_back(v); + tree[v].push_back(u); + } + + queue q{{1}}; + + while (!q.empty()) { + auto node = q.front(); + q.pop(); + + for (auto& neighbor : tree[node]) { + if (par[neighbor] == -1) { + par[neighbor] = node; + q.push(neighbor); + } + } + } + + if (par[n] == -1) { + cout << "IMPOSSIBLE\n"; + return; + } + + u32 cur = n; + vec ans; + while (cur != 1) { + ans.push_back(cur); + cur = par[cur]; + } + + ans.push_back(1); + + reverse(all(ans)); + + cout << ans.size() << '\n'; + for (u32 i = 0; i < ans.size(); ++i) { + cout << ans[i] << " \n"[i == ans.size() - 1]; + } +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/monsters.cc b/cses/graph-algorithms/monsters.cc new file mode 100644 index 0000000..ca61dc1 --- /dev/null +++ b/cses/graph-algorithms/monsters.cc @@ -0,0 +1,178 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +static map> dirs{ + {'R', {0, 1}}, {'L', {0, -1}}, {'D', {1, 0}}, {'U', {-1, 0}}}; + +void solve() { + u32 n, m; + cin >> n >> m; + + vec graph(n); + for (auto& e : graph) + cin >> e; + + queue> mq, aq; + + u32 sr, sc; + for (u32 r = 0; r < n; ++r) { + for (u32 c = 0; c < m; ++c) { + if (graph[r][c] == 'M') { + mq.emplace(r, c); + graph[r][c] = '#'; + } else if (graph[r][c] == 'A') { + sr = r, sc = c; + aq.emplace(r, c); + } + } + } + + auto valid = [&n, &m](i32 r, i32 c) -> bool { + return min(r, c) >= 0 && r < n && c < m; + }; + + vec trace(n, string(m, ' ')); + auto print_trace = [&](i32 r, i32 c) -> void { + vec path; + + while (r != sr || c != sc) { + path.push_back(trace[r][c]); + auto dir = dirs.at(trace[r][c]); + r -= dir.first; + c -= dir.second; + } + + reverse(all(path)); + cout << "YES\n" << path.size() << '\n'; + for (auto e : path) + cout << e; + }; + + while (!aq.empty()) { + u32 mcount = mq.size(); + for (u32 i = 0; i < mcount; ++i) { + auto [r, c] = mq.front(); + mq.pop(); + + for (auto& [_, dir] : dirs) { + auto nr = r + dir.first, nc = c + dir.second; + + if (!valid(nr, nc) || graph[nr][nc] == '#') { + continue; + } + + graph[nr][nc] = '#'; + mq.emplace(nr, nc); + } + } + + u32 acount = aq.size(); + for (u32 i = 0; i < acount; ++i) { + auto [r, c] = aq.front(); + aq.pop(); + + if (r == 0 || r == n - 1 || c == 0 || c == m - 1) { + print_trace(r, c); + return; + } + + for (auto& [letter, dir] : dirs) { + auto nr = r + dir.first, nc = c + dir.second; + + if (!valid(nr, nc) || trace[nr][nc] != ' ' || graph[nr][nc] == '#') { + continue; + } + + trace[nr][nc] = letter; + aq.emplace(nr, nc); + } + } + } + + NO(); +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/round-trip.cc b/cses/graph-algorithms/round-trip.cc new file mode 100644 index 0000000..6017278 --- /dev/null +++ b/cses/graph-algorithms/round-trip.cc @@ -0,0 +1,153 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + vec> tree(n + 1); + + for (u32 i = 0; i < m; ++i) { + u32 u, v; + cin >> u >> v; + + tree[u].push_back(v); + tree[v].push_back(u); + } + + vec par(n + 1, -1); + + i32 cycle_u = -1, cycle_v = -1; + + auto dfs = [&](auto&& self, u32 u, u32 parent) -> bool { + for (auto v : tree[u]) { + if (v == parent) { + continue; + } + if (par[v] != -1) { + cycle_u = u; + cycle_v = v; + return true; + } + + par[v] = u; + + if (self(self, v, u)) { + return true; + } + } + + return false; + }; + + for (u32 u = 1; u <= n; ++u) { + if (par[u] != -1) { + continue; + } + + par[u] = u; + + if (!dfs(dfs, u, u)) { + continue; + } + + vec ans; + ans.push_back(cycle_v); + for (i32 x = cycle_u; x != cycle_v; x = par[x]) + ans.push_back(x); + ans.push_back(cycle_v); + + cout << ans.size() << '\n'; + for (auto e : ans) + cout << e << ' '; + + return; + } + + cout << "IMPOSSIBLE\n"; +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/scripts/debug.sh b/cses/graph-algorithms/scripts/debug.sh new file mode 100644 index 0000000..1e63f37 --- /dev/null +++ b/cses/graph-algorithms/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" true +exit $? diff --git a/cses/graph-algorithms/scripts/run.sh b/cses/graph-algorithms/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/cses/graph-algorithms/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/graph-algorithms/scripts/utils.sh b/cses/graph-algorithms/scripts/utils.sh new file mode 100644 index 0000000..e4cf8f8 --- /dev/null +++ b/cses/graph-algorithms/scripts/utils.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + is_debug="$4" + + start=$(date '+%s.%N') + if [ -n "$is_debug" ]; then + asan="$(ldconfig -p | grep libasan.so | head -n1 | awk '{print $4}')" + LD_PRELOAD="$asan" timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + else + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + fi + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + test -n "$is_debug" && is_debug_string=true || is_debug_string=false + printf '\n[debug]: %s' "$is_debug_string" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +} diff --git a/cses/graph-algorithms/shortest-routes-i.cc b/cses/graph-algorithms/shortest-routes-i.cc new file mode 100644 index 0000000..fa6d2ba --- /dev/null +++ b/cses/graph-algorithms/shortest-routes-i.cc @@ -0,0 +1,129 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + vec delta(n + 1, MAX); + + using Edge = pair; + + vec> graph(n + 1); + + for (u32 i = 0; i < m; ++i) { + u32 u, v; + u64 w; + cin >> u >> v >> w; + graph[u].emplace_back(v, w); + } + + priority_queue, greater> pq; + pq.emplace(0, 1); + + while (!pq.empty()) { + auto [w, u] = pq.top(); + pq.pop(); + + if (w >= delta[u]) { + continue; + } + + delta[u] = w; + + for (auto& [v, W] : graph[u]) { + if (w + W < delta[v]) { + pq.emplace(w + W, v); + } + } + } + + for (u32 i = 1; i <= n; ++i) { + cout << delta[i] << " \n"[i == n]; + } +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/graph-algorithms/shortest-routes-ii.cc b/cses/graph-algorithms/shortest-routes-ii.cc new file mode 100644 index 0000000..84adc4f --- /dev/null +++ b/cses/graph-algorithms/shortest-routes-ii.cc @@ -0,0 +1,136 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m, q; + cin >> n >> m >> q; + + vec> delta(n + 1, vec(n + 1, MAX)); + + using Edge = pair; + + vec> graph(n + 1); + + for (u32 i = 0; i < m; ++i) { + u32 u, v; + u64 w; + cin >> u >> v >> w; + graph[u].emplace_back(v, w); + graph[v].emplace_back(u, w); + } + + for (u32 node = 1; node <= n; ++node) { + priority_queue, greater> pq; + pq.emplace(0, node); + + while (!pq.empty()) { + auto [w, u] = pq.top(); + pq.pop(); + + if (w >= delta[node][u]) { + continue; + } + + delta[node][u] = w; + + for (auto& [v, W] : graph[u]) { + if (w + W < delta[node][v]) { + pq.emplace(w + W, v); + } + } + } + } + + for (u32 i = 0; i < q; ++i) { + u32 u, v; + cin >> u >> v; + + cout << (delta[u][v] == MAX ? -1 : (i64)delta[u][v]) + << " \n"[i == q - 1]; + } +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); + +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + + u32 tc = 1; + // std::cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}}