From 88abce58093fcd5313aec3431a1be7742f84e037 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Fri, 10 Nov 2023 01:20:24 +0900 Subject: [PATCH 01/17] add graph_util --- .vscode/settings.json | 6 +++ cpp/graph_util.hpp | 45 +++++++++++++++++++ ...tcoder-code_festival_2017_qualb_c.test.cpp | 22 +++++++++ 3 files changed, 73 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 cpp/graph_util.hpp create mode 100644 test/atcoder-code_festival_2017_qualb_c.test.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fd4f356 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "algorithm": "cpp", + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp new file mode 100644 index 0000000..9e27dd0 --- /dev/null +++ b/cpp/graph_util.hpp @@ -0,0 +1,45 @@ +#pragma once + +/** + * @file graph.hpp + * @brief 木の汎用テンプレート + */ + +#include + +#include "graph.hpp" + +/** + * 無向グラフについて、二部グラフなら0と1に彩色した結果をひとつ返し、二部グラフでないなら空のvectorを返す。 + **/ +template +std::vector bipartite_coloring(const Graph& graph) { + std::vector color(graph.n, -1); + for (int i = 0; i < graph.n; i++) { + if (color[i] != -1) continue; + std::stack stk; + stk.push(i); + color[i] = 0; + while (!stk.empty()) { + int u = stk.top(); + stk.pop(); + for (int v : graph[u]) { + if (color[v] == -1) { + color[v] = color[u] ^ 1; + stk.push(v); + } else { + if (color[u] == color[v]) return {}; + } + } + } + } + return color; +} + +/** + * 無向グラフについて、二部グラフならtrue、二部グラフでないならfalseを返す。 + **/ +template +bool is_bipartite_graph(const Graph& graph) { + return !bipartite_coloring(graph).empty(); +} \ No newline at end of file diff --git a/test/atcoder-code_festival_2017_qualb_c.test.cpp b/test/atcoder-code_festival_2017_qualb_c.test.cpp new file mode 100644 index 0000000..ef53245 --- /dev/null +++ b/test/atcoder-code_festival_2017_qualb_c.test.cpp @@ -0,0 +1,22 @@ +#define PROBLEM "https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_c" + +#include + +#include "../cpp/graph_util.hpp" + +int main() { + int N, M; + std::cin >> N >> M; + Graph graph(N); + graph.read(M, -1); + std::vector color = bipartite_coloring(graph); + + if (color.empty()) { + std::cout << (long long)N * (N - 1) / 2 - M << std::endl; + } else { + std::array cnt = {0, 0}; + cnt[0] = count(color.begin(), color.end(), 0); + cnt[1] = count(color.begin(), color.end(), 1); + std::cout << (long long)cnt[0] * cnt[1] - M << std::endl; + } +} \ No newline at end of file From 3b0373573a7ab9bc6d153f0844b58396677c34c8 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Fri, 10 Nov 2023 01:43:52 +0900 Subject: [PATCH 02/17] change test --- test/atcoder-abc126-d.test.cpp | 27 +++++++++++++++++++ ...tcoder-code_festival_2017_qualb_c.test.cpp | 22 --------------- 2 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 test/atcoder-abc126-d.test.cpp delete mode 100644 test/atcoder-code_festival_2017_qualb_c.test.cpp diff --git a/test/atcoder-abc126-d.test.cpp b/test/atcoder-abc126-d.test.cpp new file mode 100644 index 0000000..4c9b12d --- /dev/null +++ b/test/atcoder-abc126-d.test.cpp @@ -0,0 +1,27 @@ +#define PROBLEM "https://atcoder.jp/contests/abc126/tasks/abc126_d" + +#include "../cpp/graph_util.hpp" + +int main() { + int N; + std::cin >> N; + Graph graph(2 * N); + int tmp = N; + for (int i = 0; i < N - 1; i++) { + int u, v, w; + std::cin >> u >> v >> w; + u--; + v--; + if (w % 2) { + graph.add_edge(u, tmp); + graph.add_edge(v, tmp); + tmp++; + } else { + graph.add_edge(u, v); + } + } + std::vector color = bipartite_coloring(graph); + for (int i = 0; i < N; i++) { + std::cout << color[i] << std::endl; + } +} \ No newline at end of file diff --git a/test/atcoder-code_festival_2017_qualb_c.test.cpp b/test/atcoder-code_festival_2017_qualb_c.test.cpp deleted file mode 100644 index ef53245..0000000 --- a/test/atcoder-code_festival_2017_qualb_c.test.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#define PROBLEM "https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_c" - -#include - -#include "../cpp/graph_util.hpp" - -int main() { - int N, M; - std::cin >> N >> M; - Graph graph(N); - graph.read(M, -1); - std::vector color = bipartite_coloring(graph); - - if (color.empty()) { - std::cout << (long long)N * (N - 1) / 2 - M << std::endl; - } else { - std::array cnt = {0, 0}; - cnt[0] = count(color.begin(), color.end(), 0); - cnt[1] = count(color.begin(), color.end(), 1); - std::cout << (long long)cnt[0] * cnt[1] - M << std::endl; - } -} \ No newline at end of file From 33267e1839c21c695831ceb8b13d329a4eb1a472 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Fri, 10 Nov 2023 16:07:05 +0900 Subject: [PATCH 03/17] update doxygen --- .vscode/settings.json | 6 ------ cpp/graph_util.hpp | 17 ++++++++++------- 2 files changed, 10 insertions(+), 13 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index fd4f356..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "files.associations": { - "algorithm": "cpp", - "iostream": "cpp" - } -} \ No newline at end of file diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index 9e27dd0..2377409 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -1,8 +1,8 @@ #pragma once /** - * @file graph.hpp - * @brief 木の汎用テンプレート + * @file graph_util.hpp + * @brief グラフに関する関数 */ #include @@ -10,8 +10,10 @@ #include "graph.hpp" /** - * 無向グラフについて、二部グラフなら0と1に彩色した結果をひとつ返し、二部グラフでないなら空のvectorを返す。 - **/ + * @brief 無向グラフについて、二部グラフなら0と1に彩色した結果をひとつ返し、二部グラフでないなら空のvectorを返す。 + * 連結成分のうち、インデックスの小さいものを0にする。 + * @return std::vector 各頂点の彩色結果 + */ template std::vector bipartite_coloring(const Graph& graph) { std::vector color(graph.n, -1); @@ -37,9 +39,10 @@ std::vector bipartite_coloring(const Graph& graph) { } /** - * 無向グラフについて、二部グラフならtrue、二部グラフでないならfalseを返す。 - **/ + * @brief 無向グラフについて、二部グラフかどうかを判定する。 + * @return bool 二部グラフならtrue、二部グラフでないならfalseを返す。 + */ template -bool is_bipartite_graph(const Graph& graph) { +bool is_bipartite(const Graph& graph) { return !bipartite_coloring(graph).empty(); } \ No newline at end of file From 9971570d4866a37c43f1bd5da202ca51a6e5ca63 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Fri, 10 Nov 2023 17:54:51 +0900 Subject: [PATCH 04/17] fix doxygen --- cpp/graph_util.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index 2377409..228f4a6 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -11,7 +11,7 @@ /** * @brief 無向グラフについて、二部グラフなら0と1に彩色した結果をひとつ返し、二部グラフでないなら空のvectorを返す。 - * 連結成分のうち、インデックスの小さいものを0にする。 + * 連結成分のうち、インデックスの最も小さいものを0にする。 * @return std::vector 各頂点の彩色結果 */ template @@ -45,4 +45,4 @@ std::vector bipartite_coloring(const Graph& graph) { template bool is_bipartite(const Graph& graph) { return !bipartite_coloring(graph).empty(); -} \ No newline at end of file +} From 3f55cfbba406cbca0bd7fde1c6027e88018a4077 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Fri, 10 Nov 2023 19:50:02 +0900 Subject: [PATCH 05/17] add connected_components --- cpp/graph_util.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index 228f4a6..01dacb1 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -46,3 +46,31 @@ template bool is_bipartite(const Graph& graph) { return !bipartite_coloring(graph).empty(); } + +/** + * @brief 無向グラフについて、連結成分分解する。 + * @return std::vector> 「同じ連結成分となる頂点のリスト」のリスト + */ +template +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; + std::stack stk; + stk.push(i); + visited[i] = true; + groups.push_back({i}); + while (!stk.empty()) { + int u = stk.top(); + stk.pop(); + for (int v : graph[u]) { + if (visited[v]) continue; + visited[v] = true; + stk.push(v); + groups.back().push_back(v); + } + } + } + return groups; +} \ No newline at end of file From 8ccc0abedae6312683ced59eda814a0eccf7619d Mon Sep 17 00:00:00 2001 From: shogo314 Date: Sat, 11 Nov 2023 02:25:14 +0900 Subject: [PATCH 06/17] change testcase --- test/atcoder-abc126-d.test.cpp | 27 ------------------- ...tcoder-code_festival_2017_qualb_c.test.cpp | 22 +++++++++++++++ 2 files changed, 22 insertions(+), 27 deletions(-) delete mode 100644 test/atcoder-abc126-d.test.cpp create mode 100644 test/atcoder-code_festival_2017_qualb_c.test.cpp diff --git a/test/atcoder-abc126-d.test.cpp b/test/atcoder-abc126-d.test.cpp deleted file mode 100644 index 4c9b12d..0000000 --- a/test/atcoder-abc126-d.test.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#define PROBLEM "https://atcoder.jp/contests/abc126/tasks/abc126_d" - -#include "../cpp/graph_util.hpp" - -int main() { - int N; - std::cin >> N; - Graph graph(2 * N); - int tmp = N; - for (int i = 0; i < N - 1; i++) { - int u, v, w; - std::cin >> u >> v >> w; - u--; - v--; - if (w % 2) { - graph.add_edge(u, tmp); - graph.add_edge(v, tmp); - tmp++; - } else { - graph.add_edge(u, v); - } - } - std::vector color = bipartite_coloring(graph); - for (int i = 0; i < N; i++) { - std::cout << color[i] << std::endl; - } -} \ No newline at end of file diff --git a/test/atcoder-code_festival_2017_qualb_c.test.cpp b/test/atcoder-code_festival_2017_qualb_c.test.cpp new file mode 100644 index 0000000..13303db --- /dev/null +++ b/test/atcoder-code_festival_2017_qualb_c.test.cpp @@ -0,0 +1,22 @@ +#define PROBLEM "https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_c" + +#include + +#include "../cpp/graph_util.hpp" + +int main() { + long long N, M; + std::cin >> N >> M; + Graph graph(N); + graph.read(M, -1); + std::vector color = bipartite_coloring(graph); + + if (color.empty()) { + std::cout << N * (N - 1) / 2 - M << std::endl; + } else { + std::array cnt = {0, 0}; + cnt[0] = count(color.begin(), color.end(), 0); + cnt[1] = count(color.begin(), color.end(), 1); + std::cout << cnt[0] * cnt[1] - M << std::endl; + } +} \ No newline at end of file From e6d61f40ed7665d1ff1e5e27c1c09d6b7988be11 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Sat, 11 Nov 2023 03:01:40 +0900 Subject: [PATCH 07/17] add testcase --- cpp/graph_util.hpp | 2 +- test/atcoder-abc282-d.test.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/atcoder-abc282-d.test.cpp 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 From 3fa32974801ce23f533e228445d106aef500a0ef Mon Sep 17 00:00:00 2001 From: shogo314 Date: Sat, 11 Nov 2023 03:07:20 +0900 Subject: [PATCH 08/17] remove testcase --- test/atcoder-abc282-d.test.cpp | 1 + ...tcoder-code_festival_2017_qualb_c.test.cpp | 22 ------------------- 2 files changed, 1 insertion(+), 22 deletions(-) delete mode 100644 test/atcoder-code_festival_2017_qualb_c.test.cpp diff --git a/test/atcoder-abc282-d.test.cpp b/test/atcoder-abc282-d.test.cpp index fa8ffd8..7e232fc 100644 --- a/test/atcoder-abc282-d.test.cpp +++ b/test/atcoder-abc282-d.test.cpp @@ -1,5 +1,6 @@ #define PROBLEM "https://atcoder.jp/contests/abc282/tasks/abc282_d" +#include #include #include "../cpp/graph_util.hpp" diff --git a/test/atcoder-code_festival_2017_qualb_c.test.cpp b/test/atcoder-code_festival_2017_qualb_c.test.cpp deleted file mode 100644 index 13303db..0000000 --- a/test/atcoder-code_festival_2017_qualb_c.test.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#define PROBLEM "https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_c" - -#include - -#include "../cpp/graph_util.hpp" - -int main() { - long long N, M; - std::cin >> N >> M; - Graph graph(N); - graph.read(M, -1); - std::vector color = bipartite_coloring(graph); - - if (color.empty()) { - std::cout << N * (N - 1) / 2 - M << std::endl; - } else { - std::array cnt = {0, 0}; - cnt[0] = count(color.begin(), color.end(), 0); - cnt[1] = count(color.begin(), color.end(), 1); - std::cout << cnt[0] * cnt[1] - M << std::endl; - } -} \ No newline at end of file From 40375ef5b5495de1d8c29fdf789c90fd3dc92ee5 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 10 Nov 2023 18:09:58 +0000 Subject: [PATCH 09/17] [auto-verifier] verify commit 3fa32974801ce23f533e228445d106aef500a0ef --- .verify-helper/timestamps.remote.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 12d5c99..8eb6e15 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -20,6 +20,7 @@ "test/aoj-grl-5-b.test.cpp": "2023-06-16 15:41:49 +0900", "test/atcoder-abc177-f.1.test.cpp": "2023-08-01 17:59:54 +0900", "test/atcoder-abc177-f.2.test.cpp": "2023-08-01 17:59:54 +0900", +"test/atcoder-abc282-d.test.cpp": "2023-11-11 03:07:20 +0900", "test/atcoder-abc300-b.test.cpp": "2023-09-27 09:30:35 +0900", "test/atcoder-edpc-g.test.cpp": "2023-06-06 14:52:29 +0900", "test/atcoder-past202012-n.test.cpp": "2023-08-01 18:34:30 +0900", From 1798c118664597324ea5d438ed0d2b1fecbee8da Mon Sep 17 00:00:00 2001 From: shogo314 Date: Sat, 11 Nov 2023 03:24:53 +0900 Subject: [PATCH 10/17] add is_connected --- cpp/graph_util.hpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index 4e65f0a..f6d0b8a 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -73,4 +73,13 @@ std::vector> connected_components(const Graph& graph) { } } return groups; -} \ No newline at end of file +} + +/** + * @brief 無向グラフについて、連結グラフかどうかを判定する。 + * @return bool 連結グラフならtrue、連結グラフでないならfalseを返す。 + */ +template +bool is_connected(const Graph& graph) { + return connected_components(graph).size() == 1; +} From 657d4204954f256730de147e62d37294b8368617 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 10 Nov 2023 18:28:37 +0000 Subject: [PATCH 11/17] [auto-verifier] verify commit 1798c118664597324ea5d438ed0d2b1fecbee8da --- .verify-helper/timestamps.remote.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 8eb6e15..7814ba3 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -20,7 +20,7 @@ "test/aoj-grl-5-b.test.cpp": "2023-06-16 15:41:49 +0900", "test/atcoder-abc177-f.1.test.cpp": "2023-08-01 17:59:54 +0900", "test/atcoder-abc177-f.2.test.cpp": "2023-08-01 17:59:54 +0900", -"test/atcoder-abc282-d.test.cpp": "2023-11-11 03:07:20 +0900", +"test/atcoder-abc282-d.test.cpp": "2023-11-11 03:24:53 +0900", "test/atcoder-abc300-b.test.cpp": "2023-09-27 09:30:35 +0900", "test/atcoder-edpc-g.test.cpp": "2023-06-06 14:52:29 +0900", "test/atcoder-past202012-n.test.cpp": "2023-08-01 18:34:30 +0900", From 480e1519a6646cec9d06092cfeb187b3e239521d Mon Sep 17 00:00:00 2001 From: shogo314 Date: Sat, 11 Nov 2023 14:47:19 +0900 Subject: [PATCH 12/17] add is_tree --- cpp/graph_util.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index f6d0b8a..6952202 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -83,3 +83,12 @@ template bool is_connected(const Graph& graph) { return connected_components(graph).size() == 1; } + +/** + * @brief 無向グラフについて、木かどうかを判定する。 + * @return bool 木ならtrue、木でないならfalseを返す。 + */ +template +bool is_tree(const Graph& graph) { + return graph.m == graph.n - 1 && is_connected(graph); +} From 6788bb50f3b2b479f8e1041f2545971dcedf2d82 Mon Sep 17 00:00:00 2001 From: GitHub Date: Sat, 11 Nov 2023 05:54:06 +0000 Subject: [PATCH 13/17] [auto-verifier] verify commit 480e1519a6646cec9d06092cfeb187b3e239521d --- .verify-helper/timestamps.remote.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 7814ba3..e989de3 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -20,7 +20,7 @@ "test/aoj-grl-5-b.test.cpp": "2023-06-16 15:41:49 +0900", "test/atcoder-abc177-f.1.test.cpp": "2023-08-01 17:59:54 +0900", "test/atcoder-abc177-f.2.test.cpp": "2023-08-01 17:59:54 +0900", -"test/atcoder-abc282-d.test.cpp": "2023-11-11 03:24:53 +0900", +"test/atcoder-abc282-d.test.cpp": "2023-11-11 14:47:19 +0900", "test/atcoder-abc300-b.test.cpp": "2023-09-27 09:30:35 +0900", "test/atcoder-edpc-g.test.cpp": "2023-06-06 14:52:29 +0900", "test/atcoder-past202012-n.test.cpp": "2023-08-01 18:34:30 +0900", From dd92a30844b2d1f7abec98023c280e1fb6a2ba5f Mon Sep 17 00:00:00 2001 From: shogo314 Date: Thu, 30 Nov 2023 23:29:28 +0900 Subject: [PATCH 14/17] mv topo --- .vscode/settings.json | 53 ++++++++++++++++++++++++++++++++++++ cpp/graph_util.hpp | 25 +++++++++++++++++ test/atcoder-edpc-g.test.cpp | 6 ++-- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..34f8137 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,53 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "valarray": "cpp" + } +} \ No newline at end of file diff --git a/cpp/graph_util.hpp b/cpp/graph_util.hpp index 6952202..35c6b06 100644 --- a/cpp/graph_util.hpp +++ b/cpp/graph_util.hpp @@ -92,3 +92,28 @@ template bool is_tree(const Graph& graph) { return graph.m == graph.n - 1 && is_connected(graph); } + +/** + * @brief 有向グラフをトポロジカルソートする + * @param G トポロジカルソートするグラフ + * @return ソートされたノード番号のvector DAGでなければ長さがG.n未満になる + */ +template +std::vector topological_sort(const Graph &G) { + std::vector indeg(G.n), sorted; + std::queue q; + for (int i = 0; i < G.n; i++) { + for (int dst : G[i]) indeg[dst]++; + } + for (int i = 0; i < G.n; i++) { + if (!indeg[i]) q.push(i); + } + while (!q.empty()) { + int cur = q.front(); q.pop(); + for (int dst : G[cur]) { + if (!--indeg[dst]) q.push(dst); + } + sorted.push_back(cur); + } + return sorted; +} diff --git a/test/atcoder-edpc-g.test.cpp b/test/atcoder-edpc-g.test.cpp index 4ff06e0..00a2a02 100644 --- a/test/atcoder-edpc-g.test.cpp +++ b/test/atcoder-edpc-g.test.cpp @@ -3,7 +3,7 @@ #include #include -#include "../cpp/graph.hpp" +#include "../cpp/graph_util.hpp" int main(void){ @@ -12,8 +12,8 @@ int main(void){ Graph G(N); G.read(M, -1, false, true); - std::vector dist(N); - for(int x : G.topological_sort()){ + std::vector dist(N), ord(topological_sort(G)); + for(int x : ord){ for(int y : G[x]) dist[y] = std::max(dist[y], dist[x] + 1); } std::cout << *std::max_element(dist.begin(), dist.end()) << std::endl; From 8639ed0ea480783c107902772f086d0e8b645b42 Mon Sep 17 00:00:00 2001 From: GitHub Date: Thu, 30 Nov 2023 14:36:21 +0000 Subject: [PATCH 15/17] [auto-verifier] verify commit dd92a30844b2d1f7abec98023c280e1fb6a2ba5f --- .verify-helper/timestamps.remote.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index e989de3..22cda78 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -20,9 +20,9 @@ "test/aoj-grl-5-b.test.cpp": "2023-06-16 15:41:49 +0900", "test/atcoder-abc177-f.1.test.cpp": "2023-08-01 17:59:54 +0900", "test/atcoder-abc177-f.2.test.cpp": "2023-08-01 17:59:54 +0900", -"test/atcoder-abc282-d.test.cpp": "2023-11-11 14:47:19 +0900", +"test/atcoder-abc282-d.test.cpp": "2023-11-30 23:29:28 +0900", "test/atcoder-abc300-b.test.cpp": "2023-09-27 09:30:35 +0900", -"test/atcoder-edpc-g.test.cpp": "2023-06-06 14:52:29 +0900", +"test/atcoder-edpc-g.test.cpp": "2023-11-30 23:29:28 +0900", "test/atcoder-past202012-n.test.cpp": "2023-08-01 18:34:30 +0900", "test/atcoder-past202012-n.test.py": "2023-09-07 14:26:13 +0900", "test/yosupo-binomial-coefficient.test.cpp": "2023-09-16 00:25:06 +0900", From 2cd43913857455c46d72ba1d33c20183178508e7 Mon Sep 17 00:00:00 2001 From: shogo314 Date: Thu, 30 Nov 2023 23:38:09 +0900 Subject: [PATCH 16/17] delete settings.json --- .vscode/settings.json | 53 ------------------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 34f8137..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "set": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "typeinfo": "cpp", - "valarray": "cpp" - } -} \ No newline at end of file From ae253f1f1ce2892f85fc6854aa24c1df92516531 Mon Sep 17 00:00:00 2001 From: GitHub Date: Wed, 3 Jan 2024 04:50:23 +0000 Subject: [PATCH 17/17] [auto-verifier] verify commit ed920623f9bf122d6f083036b5701b3440964009 --- .verify-helper/timestamps.remote.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index df98586..5192d41 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -21,7 +21,7 @@ "test/atcoder-abc177-f.1.test.cpp": "2023-08-01 17:59:54 +0900", "test/atcoder-abc177-f.2.test.cpp": "2023-08-01 17:59:54 +0900", "test/atcoder-abc282-d.test.cpp": "2023-11-30 23:29:28 +0900", -"test/atcoder-abc300-b.test.cpp": "2023-09-27 09:30:35 +0900", +"test/atcoder-abc300-b.test.cpp": "2023-10-12 08:50:40 +0900", "test/atcoder-edpc-g.test.cpp": "2023-11-30 23:29:28 +0900", "test/atcoder-past202012-n.test.cpp": "2023-08-01 18:34:30 +0900", "test/atcoder-past202012-n.test.py": "2023-09-07 14:26:13 +0900",