Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level Generation Editorial #4966

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions content/3_Silver/Binary_Search.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@
"isStarred": false,
"tags": ["Binary Search"],
"solutionMetadata": {
"kind": "autogen-label-from-site",
"site": "CF"
"kind": "internal",
"hasHints": true
}
},
{
Expand Down
79 changes: 79 additions & 0 deletions solutions/silver/cf-818F.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
id: cf-818F
source: CF
title: Level Generation
author: Justin Ji
---

<Spoiler title="Hint 1">

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.
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved

</Spoiler>

<Spoiler title="Hint 2">

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?

</Spoiler>

<Spoiler title="Explanation">

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
$\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. 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

**Time Complexity:** $\mathcal{O}(Q\log{N})$

<LanguageSection>
<CPPSection>

```cpp
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
int test_num;
cin >> test_num;
for (int i = 0; i < test_num; i++) {
TheGamingMousse marked this conversation as resolved.
Show resolved Hide resolved
int nodes;
cin >> nodes;

ll low = nodes - 1;
ll high = 2ll * (nodes - 1);
while (low < high) {
ll mid = (low + high + 1) / 2;
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;
}
}
cout << low << '\n';
}
}
```

</CPPSection>
</LanguageSection>

</Spoiler>
Loading