Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
11a9163
Change solutionMetadata kind to internal
Kiruthees Feb 4, 2026
d5947ca
Added internal solution for AC-Grouping as it did not have editorial …
Kiruthees Feb 4, 2026
f6adfcf
Merge branch 'cpinitiative:master' into Cses2421
Kiruthees Feb 6, 2026
ef46aeb
Revise ac-grouping.mdx with new structure and content
Kiruthees Feb 6, 2026
7b6a081
Fix formatting and improve clarity in ac-grouping.mdx
Kiruthees Feb 6, 2026
e75fe60
Fix formatting and clarify explanation in ac-grouping.mdx
Kiruthees Feb 7, 2026
4f9ea1f
minor fixes
Kiruthees Feb 7, 2026
e896c67
Merge branch 'master' into Cses2421
Kiruthees Feb 7, 2026
2f6f4ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2026
d80062b
Revise explanation and implementation for AC grouping
Kiruthees Feb 8, 2026
e3bd21e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 8, 2026
19232a7
Update solutions/gold/ac-grouping.mdx
Kiruthees Feb 10, 2026
89164d6
resolved conversation
Kiruthees Feb 10, 2026
38a06af
Merge branch 'master' into Cses2421
Kiruthees Feb 10, 2026
a8320a4
Update solutions/gold/ac-grouping.mdx
Kiruthees Feb 11, 2026
ef2f53e
committed formatting changes based on conversation
Kiruthees Feb 11, 2026
492e0dc
made necessary changes as per eysbutno
Kiruthees Feb 12, 2026
c5ad0a6
Merge branch 'master' into Cses2421
Kiruthees Feb 12, 2026
b29b808
Merge branch 'master' into Cses2421
Kiruthees Feb 12, 2026
22d5667
Merge branch 'master' into Cses2421
Kiruthees Feb 13, 2026
7f01107
added explanation for why xor works here
Kiruthees Feb 14, 2026
828d43b
Update ac-grouping.mdx
eysbutno Feb 14, 2026
37c5582
Merge branch 'master' into Cses2421
eysbutno Feb 14, 2026
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
3 changes: 1 addition & 2 deletions content/4_Gold/DP_Bitmasks.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"isStarred": false,
"tags": ["Bitmasks", "DP"],
"solutionMetadata": {
"kind": "autogen-label-from-site",
"site": "AC"
"kind": "internal"
}
},
{
Expand Down
77 changes: 77 additions & 0 deletions solutions/gold/ac-grouping.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
id: ac-grouping
source: AtCoder
title: Grouping
author: Kiruthees G
---

## Explanation

TThe goal is to partition a set of $N$ rabbits into any number of groups to maximize the total compatibility score. Since $N$ is small (up to 16), we use Bitmask DP.

### 1. Bitmask Representation

An integer $i$ between $0$ and $2^N - 1$ represents a subset of rabbits. If the $j$-th bit is set ($1$), rabbit $j$ is included in the subset.

### 2. DP State and Transition

We define `dp[i]` as the maximum score possible for the subset of rabbits represented by mask $i$.

The transition involves two steps for each mask $i$:

1. **Base Score**: Initialize `dp[i]` by calculating the sum of $a_{j,k}$ for all pairs $(j, k)$ within the mask. This assumes all rabbits in the mask form a single group.
2. **Merging Subsets**: Improve `dp[i]` by splitting the mask into two disjoint submasks $j$ and $i \char`\^ j$. The transition is:

$$dp[i] = \max_{j \subseteq i} \{ dp[j] + dp[i \char`\^ j] \}$$

For an efficient way to iterate over submasks, we use the pattern `for (int j = i; j; j = (j - 1) & i)`. You can read more about this technique in the [USACO Guide: Merging Subsets](https://usaco.guide/gold/dp-bitmasks?lang=cpp#merging-subsets).

### Additional Resources

* [USACO Guide: Bitmask DP](https://usaco.guide/gold/dp-bitmasks?lang=cpp)
* [Unofficial Editorial (Nwatx)](https://nwatx.me/post/atcoderdp)

---

## Implementation

**Time Complexity**: $$\mathcal{O}(3^N + N^2 \cdot 2^N)$$

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

using ll = long long;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int n;
cin >> n;
vector a(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { cin >> a[i][j]; }
}

vector<ll> dp(1 << n);
// Iterate through every possible subset of rabbits
for (int i = 1; i < (1 << n); i++) {
// Calculate internal score if this subset forms ONE single group
for (int j = 0; j < n; j++) {
if (!(i >> j & 1)) continue;
for (int k = j + 1; k < n; k++) {
if (!(i >> k & 1)) continue;
dp[i] += a[j][k];
}
}

// Try splitting the mask into smaller groups to find a better score
for (int j = (i - 1) & i; j > 0; j = (j - 1) & i) {
dp[i] = max(dp[i], dp[j] + dp[i ^ j]);
}
}

cout << dp.back() << '\n';
}
```