From 96254947699986c59f0dc63d69fd4b76bd3ed43e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 6 Jul 2026 19:08:08 +0200 Subject: Initial commit --- 04_graph_algorithms/building_teams_1668.cpp | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 04_graph_algorithms/building_teams_1668.cpp (limited to '04_graph_algorithms/building_teams_1668.cpp') diff --git a/04_graph_algorithms/building_teams_1668.cpp b/04_graph_algorithms/building_teams_1668.cpp new file mode 100644 index 0000000..c98e14d --- /dev/null +++ b/04_graph_algorithms/building_teams_1668.cpp @@ -0,0 +1,37 @@ +#include +#include + +bool visit(size_t i, int c, const std::vector>& a, + std::vector& t) { + if (t[i] != 0) return true; + t[i] = c; + for (auto j : a[i]) + if (t[j] == c) + return false; + else if (!visit(j, 3-c, a, t)) return false; + return true; +} + +int main() { + size_t n, m; + std::cin >> n >> m; + std::vector> a(n); + for (size_t i = 0; i < m; i++) { + size_t x, y; + std::cin >> x >> y; + a[x-1].push_back(y-1); + a[y-1].push_back(x-1); + } + + std::vector t(n); + for (size_t i = 0; i < n; i++) { + if (t[i] == 0) { + if (!visit(i, 1, a, t)) { + std::cout << "IMPOSSIBLE\n"; + return 0; + } + } + } + for (auto x : t) std::cout << x << " "; + std::cout << "\n"; +} -- cgit v1.3