diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index 01dacb1..4e65f0a 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -56,7 +56,7 @@ std::vector> connected_components(const Graph& graph) { std::vector> groups; std::vector visited(graph.n); for (int i = 0; i < graph.n; i++) { - if (color[i] != -1) continue; + if (visited[i]) continue; std::stack stk; stk.push(i); visited[i] = true; diff --git a/test/atcoder-abc282-d.test.cpp b/test/atcoder-abc282-d.test.cpp new file mode 100644 index 0000000..fa8ffd8 --- /dev/null +++ b/test/atcoder-abc282-d.test.cpp @@ -0,0 +1,26 @@ +#define PROBLEM "https://atcoder.jp/contests/abc282/tasks/abc282_d" + +#include + +#include "../cpp/graph_util.hpp" + +int main(void) { + int N, M; + std::cin >> N >> M; + Graph graph(N); + graph.read(M); + std::vector c = bipartite_coloring(graph); + if (c.empty()) { + std::cout << 0 << std::endl; + return 0; + } + std::vector> groups = connected_components(graph); + long long ans = (long long)N * (N - 1) / 2 - M; + for (const std::vector& group : groups) { + std::array wb = {}; + std::for_each(group.begin(), group.end(), [&](int x) { wb[c[x]]++; }); + auto [w, b] = wb; + ans -= w * (w - 1) / 2 + b * (b - 1) / 2; + } + std::cout << ans << std::endl; +} \ No newline at end of file