Official

D - 接続容器の色配置 / Color Arrangement in Connected Containers Editorial by admin

claude5.5opus-high

Overview

If we consider containers connected by exchange devices as a single group (connected component), we can freely rearrange the colors within each group. Therefore, the answer is obtained by finding the “number of ways to arrange the colors while preserving the number of reds,” \(\binom{\text{size}}{\text{number of reds}}\), for each group and multiplying them all together.

Analysis

Modeling as a Graph

Consider a graph where the containers are vertices and the exchange devices are edges. An exchange is an operation that swaps the contents of two vertices connected by an edge. Therefore, liquid cannot move outside the same connected component. We can see that the number of red and blue liquids contained in each connected component remains unchanged regardless of the operations.

Key Insight: Colors Can Be Freely Rearranged Within a Connected Component

Within a connected component, any arrangement can be achieved as long as the numbers of red and blue liquids are preserved.

The reason is as follows. Take a spanning tree of the connected component. Choose a leaf \(\ell\) of the tree, and find a vertex \(x\) that contains liquid of the color we want to place at \(\ell\). By performing swaps along the path on the tree from \(x\) to \(\ell\), \(x = p_0, p_1, \dots, p_k = \ell\), in the order \((p_0,p_1), (p_1,p_2), \dots\), we can move that liquid to \(\ell\). Once it is moved, we fix \(\ell\) so it is no longer moved, and remove it from the tree. Since removing a leaf leaves the remaining graph as a tree (still connected), repeating this procedure allows us to place the desired colors at all vertices.

Counting

In a connected component of size \(k\) with \(r\) red liquids, we only need to choose which \(r\) of the \(k\) positions will be red. The number of ways to do this is \(\binom{k}{r}\). Since different connected components do not affect each other, the answer is

\[\prod_{\text{connected component } C} \binom{|C|}{r_C}\]

.

Example: Consider the case where \(N=4\), \(S=\) 1010, and the edges are \((1,2),(2,3)\). - Component \(\{1,2,3\}\) has colors 1,0,1, size \(3\), and \(2\) reds, giving \(\binom{3}{2}=3\) ways (110, 101, 011). - Component \(\{4\}\) has \(\binom{1}{0}=1\) way. - The answer is \(3 \times 1 = 3\).

Why the Naive Approach Fails

If we enumerate reachable states using BFS or similar methods, the number of states can be up to \(2^N\), which will not run in time at all. Based on the observation above, it suffices to simply count the “size” and the “number of reds” for each connected component.

Algorithm

  1. Sequentially merge edges using Union-Find (DSU) to find the connected components. Self-loops \(U_j=V_j\) can be ignored. Multi-edges do not change anything when merged, so they do not require special attention.
  2. Find the representative (leader) for each vertex, and aggregate the component size \(sz\) and the number of reds \(red\) for each representative.
  3. Precompute factorials \(f[i]=i!\) and inverse factorials \(inv[i]=(i!)^{-1}\) \(\bmod 998244353\).
  4. For each component, compute \(\binom{sz}{red} = f[sz]\cdot inv[red]\cdot inv[sz-red]\) and multiply them together.

Complexity

  • Time complexity: \(O((N+M)\,\alpha(N) + \log \text{MOD})\)
  • Space complexity: \(O(N)\)

Implementation Notes

  • For modular inverses, use Fermat’s Little Theorem to compute \((N!)^{-1} = (N!)^{\text{MOD}-2}\) once using binary exponentiation. Then, by filling backwards with \(inv[i-1] = inv[i]\cdot i\), all inverse factorials can be obtained in \(O(N)\).

  • Aggregation is performed on the indices of the representatives. Finally, by multiplying only the positions where \(sz[i]>0\), each component is counted exactly once.

  • When \(M=0\), there is no edge input. In this case, all vertices are isolated components, and the answer is \(1\).

    Source Code

#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;
const long long MOD=998244353;
long long pw(long long b,long long e){long long r=1;b%=MOD;while(e){if(e&1)r=r*b%MOD;b=b*b%MOD;e>>=1;}return r;}
int main(){
    int n,m;
    if(!(cin>>n>>m))return 0;
    string s;cin>>s;
    atcoder::dsu d(n);
    for(int j=0;j<m;j++){int u,v;cin>>u>>v;u--;v--;if(u!=v)d.merge(u,v);}
    vector<long long> f(n+1),inv(n+1);
    f[0]=1;for(int i=1;i<=n;i++)f[i]=f[i-1]*i%MOD;
    inv[n]=pw(f[n],MOD-2);for(int i=n;i>0;i--)inv[i-1]=inv[i]*i%MOD;
    vector<int> sz(n,0),red(n,0);
    for(int i=0;i<n;i++){int r=d.leader(i);sz[r]++;if(s[i]=='1')red[r]++;}
    long long ans=1;
    for(int i=0;i<n;i++){
        if(sz[i]>0){
            ans=ans*f[sz[i]]%MOD*inv[red[i]]%MOD*inv[sz[i]-red[i]]%MOD;
        }
    }
    cout<<ans<<endl;
}

This editorial was generated by claude5.5opus-high.