cses graphs

This commit is contained in:
Barrett Ruth 2025-08-31 19:16:59 -05:00
parent 98ea573f5a
commit 02046fbdf6
35 changed files with 1645 additions and 0 deletions

View file

@ -0,0 +1,9 @@
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortCompoundRequirementOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false

View file

@ -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

View file

@ -0,0 +1,130 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<vec<u32>> 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<u32> 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;
}
// }}}

View file

@ -0,0 +1,137 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<i32> color(n + 1, -1);
vec<vec<u32>> 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<u32> 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;
}
// }}}

View file

@ -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

View file

@ -0,0 +1,122 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<string> 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;
}
// }}}

View file

@ -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

View file

@ -0,0 +1,3 @@
4 2
3 1
2 4

View file

@ -0,0 +1,6 @@
1
1 2
[code]: 0
[time]: 3.44825 ms
[debug]: false

View file

@ -0,0 +1,4 @@
5 3
1 2
1 3
4 5

View file

@ -0,0 +1,5 @@
1 2 2 1 2
[code]: 0
[time]: 2.27332 ms
[debug]: false

View file

@ -0,0 +1,6 @@
5 8
########
#..#...#
####.#.#
#..#...#
########

View file

@ -0,0 +1,5 @@
3
[code]: 0
[time]: 4.39477 ms
[debug]: false

View file

@ -0,0 +1,6 @@
5 8
########
#.A#...#
#.##.#B#
#......#
########

View file

@ -0,0 +1,7 @@
YES
9
LDDRRRRRU
[code]: 0
[time]: 3.1817 ms
[debug]: false

View file

@ -0,0 +1,6 @@
5 5
1 2
1 3
1 4
2 3
5 4

View file

@ -0,0 +1,6 @@
3
1 4 5
[code]: 0
[time]: 2.26545 ms
[debug]: false

View file

@ -0,0 +1,15 @@
14 7
#######
#.....#
#.###.#
#.###.#
#.....#
#A###..
#####.#
#####.#
#####.#
#####.#
#####.#
#####.#
#####.#
#M....#

View file

@ -0,0 +1,6 @@
YES
7
URRRRDR
[code]: 0
[time]: 2.22921 ms
[debug]: false

View file

@ -0,0 +1,7 @@
5 6
1 3
1 2
5 3
1 5
2 4
4 5

View file

@ -0,0 +1,5 @@
4
1 5 3 1
[code]: 0
[time]: 2.29764 ms
[debug]: false

View file

@ -0,0 +1,6 @@
3 4
1 2 6
1 3 2
3 2 3
1 3 4

View file

@ -0,0 +1,5 @@
0 5 2
[code]: 0
[time]: 2.31624 ms
[debug]: false

View file

@ -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

View file

@ -0,0 +1,5 @@
5 5 8 -1 3
[code]: 0
[time]: 2.57158 ms
[debug]: false

View file

@ -0,0 +1,157 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<char, pair<i32, i32>> dirs{
{'L', {0, -1}}, {'R', {0, 1}}, {'U', {-1, 0}}, {'D', {1, 0}}};
void solve() {
u32 n, m;
cin >> n >> m;
vec<string> 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<pair<i32, i32>> q{{{sr, sc}}};
vec<string> 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;
}
// }}}

View file

@ -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
%:
@:

View file

@ -0,0 +1,139 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<vec<u32>> tree(n + 1);
vec<i32> 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<u32> 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<u32> 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;
}
// }}}

View file

@ -0,0 +1,178 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<char, pair<i32, i32>> dirs{
{'R', {0, 1}}, {'L', {0, -1}}, {'D', {1, 0}}, {'U', {-1, 0}}};
void solve() {
u32 n, m;
cin >> n >> m;
vec<string> graph(n);
for (auto& e : graph)
cin >> e;
queue<pair<i32, i32>> 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<string> trace(n, string(m, ' '));
auto print_trace = [&](i32 r, i32 c) -> void {
vec<char> 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;
}
// }}}

View file

@ -0,0 +1,153 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<vec<u32>> 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<i32> 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<i32> 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;
}
// }}}

View file

@ -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 $?

View file

@ -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 $?

View file

@ -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
}

View file

@ -0,0 +1,129 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<u64> delta(n + 1, MAX<u64>);
using Edge = pair<u64, u32>;
vec<vec<Edge>> 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<Edge, vector<Edge>, greater<Edge>> 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;
}
// }}}

View file

@ -0,0 +1,136 @@
#include <bits/stdc++.h> // {{{
#include <version>
#ifdef __cpp_lib_ranges_enumerate
#include <ranges>
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 <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(x.size());
}
#endif
static void NO() {
std::cout << "NO\n";
}
static void YES() {
std::cout << "YES\n";
}
template <typename T>
using vec = std::vector<T>;
#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<vec<u64>> delta(n + 1, vec<u64>(n + 1, MAX<u64>));
using Edge = pair<u64, u32>;
vec<vec<Edge>> 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<Edge, vector<Edge>, greater<Edge>> 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<u64> ? -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;
}
// }}}