From 44abd7d91aaefffc60d755b1d12239fe6678c1be Mon Sep 17 00:00:00 2001 From: TheGamingMousse <68484800+TheGamingMousse@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:23:31 -0800 Subject: [PATCH 1/9] Level Generation Editorial --- content/3_Silver/Binary_Search.problems.json | 4 +- solutions/silver/cf-818F.mdx | 90 ++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 solutions/silver/cf-818F.mdx diff --git a/content/3_Silver/Binary_Search.problems.json b/content/3_Silver/Binary_Search.problems.json index 16087c6d76..54a46e252c 100644 --- a/content/3_Silver/Binary_Search.problems.json +++ b/content/3_Silver/Binary_Search.problems.json @@ -324,8 +324,8 @@ "isStarred": false, "tags": ["Binary Search"], "solutionMetadata": { - "kind": "autogen-label-from-site", - "site": "CF" + "kind": "internal", + "hasHints": true } }, { diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx new file mode 100644 index 0000000000..6805631112 --- /dev/null +++ b/solutions/silver/cf-818F.mdx @@ -0,0 +1,90 @@ +--- +id: cf-818F +source: CF +title: Level Generation +author: Justin Ji +--- + + + +What is the upper bound for the number of bridges we can have? + + + + + +The optimal construction would be to create a tree, which has $n - 1$ bridges. + + + + + +Let's consider allocating a certain amount of these nodes to forming a tree. +How can we use the rest of these nodes to use as many edges as possible? + + + + + +The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. For +those interested, the reason this is the case is that all of our bridges must be part of an +arbitrary spanning tree of our graph. + +As a result, our answer is in the range $[n - 1, 2n - 2]$. Let's consider binary +searching on our answer. If we have $x$ edges that we need to use, then +$\lfloor \frac{x + 1}{2} \rfloor$ of these edges must be a bridge. + +Recall that the best way to create bridges is to create a tree. Thus, we use all of these +bridge edges to form a tree, and then use the one extra edge to connect this tree +to some component of nodes. With the rest of our nodes, we can form a complete +graph of nodes to use as many edges as possible. + +## Implementation + +**Time Complexity:** $\mathcal{O}(Q\log{N})$ + +^ Format time complexity like this. Should appear outside of `` if it's the same for all implementations. + + + + +```cpp +#include +using namespace std; + +using ll = long long; + +int main() { + int test_num; + cin >> test_num; + for (int i = 0; i < test_num; i++) { + int nodes; + cin >> nodes; + + /** @return if we can make a graph with this many edges */ + const auto check = [&](ll edges) -> bool { + int num_bridges = (edges + 1) / 2; + int cycle_nodes = nodes - num_bridges; + ll cycle_edges = 1ll * cycle_nodes * (cycle_nodes - 1) / 2; + return edges - num_bridges <= cycle_edges; + }; + + ll low = nodes - 1; + ll high = 2ll * (nodes - 1); + while (low < high) { + ll mid = (low + high + 1) / 2; + if (check(mid)) { + low = mid; + } else { + high = mid - 1; + } + } + cout << low << '\n'; + } +} +``` + + + + + \ No newline at end of file From 6b28c324744115df306c1831adaf5d003a016e76 Mon Sep 17 00:00:00 2001 From: Justin Ji <68484800+TheGamingMousse@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:25:58 -0800 Subject: [PATCH 2/9] Update cf-818F.mdx --- solutions/silver/cf-818F.mdx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 6805631112..0fc5608556 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -43,8 +43,6 @@ graph of nodes to use as many edges as possible. **Time Complexity:** $\mathcal{O}(Q\log{N})$ -^ Format time complexity like this. Should appear outside of `` if it's the same for all implementations. - @@ -87,4 +85,4 @@ int main() { - \ No newline at end of file + From 0213171d5f84c415dedd32b4f159eaee2231157c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Dec 2024 01:26:12 +0000 Subject: [PATCH 3/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-818F.mdx | 70 ++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 0fc5608556..7e9e32b866 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -26,17 +26,17 @@ How can we use the rest of these nodes to use as many edges as possible? -The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. For -those interested, the reason this is the case is that all of our bridges must be part of an -arbitrary spanning tree of our graph. +The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. For +those interested, the reason this is the case is that all of our bridges must be part of an +arbitrary spanning tree of our graph. -As a result, our answer is in the range $[n - 1, 2n - 2]$. Let's consider binary -searching on our answer. If we have $x$ edges that we need to use, then -$\lfloor \frac{x + 1}{2} \rfloor$ of these edges must be a bridge. +As a result, our answer is in the range $[n - 1, 2n - 2]$. Let's consider binary +searching on our answer. If we have $x$ edges that we need to use, then +$\lfloor \frac{x + 1}{2} \rfloor$ of these edges must be a bridge. -Recall that the best way to create bridges is to create a tree. Thus, we use all of these -bridge edges to form a tree, and then use the one extra edge to connect this tree -to some component of nodes. With the rest of our nodes, we can form a complete +Recall that the best way to create bridges is to create a tree. Thus, we use all of these +bridge edges to form a tree, and then use the one extra edge to connect this tree +to some component of nodes. With the rest of our nodes, we can form a complete graph of nodes to use as many edges as possible. ## Implementation @@ -53,32 +53,32 @@ using namespace std; using ll = long long; int main() { - int test_num; - cin >> test_num; - for (int i = 0; i < test_num; i++) { - int nodes; - cin >> nodes; - - /** @return if we can make a graph with this many edges */ - const auto check = [&](ll edges) -> bool { - int num_bridges = (edges + 1) / 2; - int cycle_nodes = nodes - num_bridges; - ll cycle_edges = 1ll * cycle_nodes * (cycle_nodes - 1) / 2; - return edges - num_bridges <= cycle_edges; - }; - - ll low = nodes - 1; - ll high = 2ll * (nodes - 1); - while (low < high) { - ll mid = (low + high + 1) / 2; - if (check(mid)) { - low = mid; - } else { - high = mid - 1; - } - } - cout << low << '\n'; - } + int test_num; + cin >> test_num; + for (int i = 0; i < test_num; i++) { + int nodes; + cin >> nodes; + + /** @return if we can make a graph with this many edges */ + const auto check = [&](ll edges) -> bool { + int num_bridges = (edges + 1) / 2; + int cycle_nodes = nodes - num_bridges; + ll cycle_edges = 1ll * cycle_nodes * (cycle_nodes - 1) / 2; + return edges - num_bridges <= cycle_edges; + }; + + ll low = nodes - 1; + ll high = 2ll * (nodes - 1); + while (low < high) { + ll mid = (low + high + 1) / 2; + if (check(mid)) { + low = mid; + } else { + high = mid - 1; + } + } + cout << low << '\n'; + } } ``` From 6a748dadeaa7feac37feaf58e89bddf937357988 Mon Sep 17 00:00:00 2001 From: TheGamingMousse <68484800+TheGamingMousse@users.noreply.github.com> Date: Fri, 6 Dec 2024 18:37:31 -0800 Subject: [PATCH 4/9] refactored code a bit --- solutions/silver/cf-818F.mdx | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 7e9e32b866..70eaf3fb9f 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -59,19 +59,15 @@ int main() { int nodes; cin >> nodes; - /** @return if we can make a graph with this many edges */ - const auto check = [&](ll edges) -> bool { - int num_bridges = (edges + 1) / 2; - int cycle_nodes = nodes - num_bridges; - ll cycle_edges = 1ll * cycle_nodes * (cycle_nodes - 1) / 2; - return edges - num_bridges <= cycle_edges; - }; - ll low = nodes - 1; ll high = 2ll * (nodes - 1); while (low < high) { ll mid = (low + high + 1) / 2; - if (check(mid)) { + int num_bridges = (mid + 1) / 2; + int cycle_nodes = nodes - num_bridges; + ll cycle_edges = 1ll * cycle_nodes * (cycle_nodes - 1) / 2; + + if (mid - num_bridges <= cycle_edges) { low = mid; } else { high = mid - 1; From 7366fb3b91caf48b7968d40de220754d0b7e60b9 Mon Sep 17 00:00:00 2001 From: Justin Ji <68484800+TheGamingMousse@users.noreply.github.com> Date: Fri, 6 Dec 2024 22:03:59 -0800 Subject: [PATCH 5/9] Update cf-818F.mdx --- solutions/silver/cf-818F.mdx | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 70eaf3fb9f..ad2ca54dcb 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -7,13 +7,8 @@ author: Justin Ji -What is the upper bound for the number of bridges we can have? - - - - - -The optimal construction would be to create a tree, which has $n - 1$ bridges. +The upper bound for the number of bridges we can have is $n - 1$. To achieve +this, we construct a tree. From 84e9911258e1739ac288a812ccb1d29c7fd152e0 Mon Sep 17 00:00:00 2001 From: Justin Ji <68484800+TheGamingMousse@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:37:27 -0800 Subject: [PATCH 6/9] Update cf-818F.mdx --- solutions/silver/cf-818F.mdx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index ad2ca54dcb..1bf1a943b8 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -7,8 +7,9 @@ author: Justin Ji -The upper bound for the number of bridges we can have is $n - 1$. To achieve -this, we construct a tree. +The upper bound for the number of bridges we can have is $n - 1$ because +each bridge is present in any spanning tree of the graph. A +spanning tree is a subgraph that connects every node without cycles. @@ -21,17 +22,16 @@ How can we use the rest of these nodes to use as many edges as possible? -The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. For -those interested, the reason this is the case is that all of our bridges must be part of an -arbitrary spanning tree of our graph. +The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. +As a result, our answer is in the range $[n - 1, 2n - 2]$. -As a result, our answer is in the range $[n - 1, 2n - 2]$. Let's consider binary +Let's consider binary searching on our answer. If we have $x$ edges that we need to use, then -$\lfloor \frac{x + 1}{2} \rfloor$ of these edges must be a bridge. +$\lfloor \frac{x + 1}{2} \rfloor$ of these edges must be bridges. Recall that the best way to create bridges is to create a tree. Thus, we use all of these bridge edges to form a tree, and then use the one extra edge to connect this tree -to some component of nodes. With the rest of our nodes, we can form a complete +to some component of nodes. Note that this extra edge is also a bridge. With the rest of our nodes, we can form a complete graph of nodes to use as many edges as possible. ## Implementation From 5d225069440d0e1368c60c7b2c9f8c28e2e66279 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Dec 2024 02:38:39 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-818F.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 1bf1a943b8..130fc77177 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -7,8 +7,8 @@ author: Justin Ji -The upper bound for the number of bridges we can have is $n - 1$ because -each bridge is present in any spanning tree of the graph. A +The upper bound for the number of bridges we can have is $n - 1$ because +each bridge is present in any spanning tree of the graph. A spanning tree is a subgraph that connects every node without cycles. @@ -22,8 +22,8 @@ How can we use the rest of these nodes to use as many edges as possible? -The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. -As a result, our answer is in the range $[n - 1, 2n - 2]$. +The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. +As a result, our answer is in the range $[n - 1, 2n - 2]$. Let's consider binary searching on our answer. If we have $x$ edges that we need to use, then From 54beac78a1be0a5fceb2a7532a12d34fb7b4dc8b Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:18:19 -0800 Subject: [PATCH 8/9] Update cf-818F.mdx --- solutions/silver/cf-818F.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 130fc77177..80e65df64c 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -68,6 +68,7 @@ int main() { high = mid - 1; } } + cout << low << '\n'; } } From 1e9d78196fe9a9d6478882e99596821f948d7202 Mon Sep 17 00:00:00 2001 From: Justin Ji <68484800+TheGamingMousse@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:21:04 -0800 Subject: [PATCH 9/9] Update cf-818F.mdx --- solutions/silver/cf-818F.mdx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/solutions/silver/cf-818F.mdx b/solutions/silver/cf-818F.mdx index 80e65df64c..85073878fa 100644 --- a/solutions/silver/cf-818F.mdx +++ b/solutions/silver/cf-818F.mdx @@ -5,11 +5,12 @@ title: Level Generation author: Justin Ji --- +[Official Analysis](https://codeforces.com/blog/entry/52991) + The upper bound for the number of bridges we can have is $n - 1$ because -each bridge is present in any spanning tree of the graph. A -spanning tree is a subgraph that connects every node without cycles. +each bridge is present in any [spanning tree](https://en.wikipedia.org/wiki/Spanning_tree) of the graph.