---
id: 1525
lang: en
translation_of: index.md
source_sha256: 10468c6172d4869ac11df12cf9d7b181e298c8f4df4b8aedd5271c1b7b450e41
title: "[Fortuna OJ] Aug 1st - Group A Problem-Solving Report"
description: "A uses IDA* with the number of remaining colors as the heuristic; B uses Floyd transitive closure plus bipartite matching; C is a counting DP."
tags: ["OI","Math","Graph Theory","DP","Binomial Coefficients","Bipartite Graph","Network Flow","IDA*","A*","Floyd","Transitive Closure","Minimum Vertex Cover"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "玄妙重重 is a set literary phrase, not slang; rendered literally as “truly subtle, layer upon layer” with no chip."
  - "注意控制 BFS 求连通块的个数的优化 is ambiguous; read as limiting how many times the connected-component BFS is run."
  - "Problem names romanized or translated: 水叮当的舞步 → Shui Dingdang's Dance Steps, 粉刷匠 → The Painter."
---

## A - Shui Dingdang's Dance Steps

Truly subtle, layer upon layer.

Let's first think about the ordinary brute-force search: enumerate the color pressed each time, then check and keep updating. Consider optimizing this process with IDA\*. First, the heuristic function is just the number of remaining colors, because at least these colors have to be updated before the final state can possibly be reached. Then just pay attention to the optimization of controlling how many times BFS is run to find connected components. A really nice problem.

<!--more-->

```cpp
// A.cpp
#include <bits/stdc++.h>
#define pr pair<int, int>

using namespace std;

const int MAX_N = 10;
const int dirx[4] = {0, 0, -1, 1}, diry[4] = {-1, 1, 0, 0};

int n, mp[MAX_N][MAX_N], tail;
bool connected[MAX_N][MAX_N], col[MAX_N];

pr q[MAX_N << 15];

void bfs(int color)
{
    for (int i = 1; i <= tail; i++)
    {
        int x = q[i].first, y = q[i].second;
        for (int dir = 0; dir < 4; dir++)
        {
            int dstx = x + dirx[dir], dsty = y + diry[dir];
            if (dstx <= n && dstx >= 1 && dsty <= n && dsty >= 1 && mp[dstx][dsty] == color && connected[dstx][dsty] == false)
            {
                connected[dstx][dsty] = true;
                q[++tail] = make_pair(dstx, dsty);
            }
        }
    }
}

bool dfs(int dep, int limit)
{
    if (tail == n * n)
        return true;
    memset(col, 0, sizeof(col));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (connected[i][j] == false)
                col[mp[i][j]] = true;
    int tt = 0;
    for (int i = 0; i <= 5; i++)
        if (col[i])
            tt++;
    if (tt + dep > limit)
        return false;
    int tp = tail;
    for (int i = 0; i <= 5; i++)
    {
        bfs(i);
        if (tail == tp)
            continue;
        if (dfs(dep + 1, limit))
            return true;
        for (int j = tp + 1; j <= tail; j++)
            connected[q[j].first][q[j].second] = false;
        tail = tp;
    }
    return false;
}

int main()
{
    while (scanf("%d", &n) && n != 0)
    {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", &mp[i][j]);
        bool flag = true;
        for (int limit = 0; limit <= 64; limit++)
        {
            memset(connected, 0, sizeof(connected));
            q[1] = make_pair(1, 1);
            connected[1][1] = true;
            tail = 1;
            bfs(mp[1][1]);
            if (dfs(0, limit))
            {
                printf("%d\n", limit);
                break;
            }
        }
    }
    return 0;
}
```

## B - Vani and Cl2 Play Hide-and-Seek

First use Floyd to compute the transitive closure, then enumerate all the vertices and connect the reachable vertices on a new network (that is, a bipartite graph). After the bipartite matching, just subtract the max flow from $n$.

```cpp
// B.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 510, INF = 0x3f3f3f3f;

int n, m, head[MAX_N], current, cur[MAX_N], st, ed, dep[MAX_N];
bool mp[MAX_N][MAX_N];

struct edge
{
    int to, nxt, weight;
} edges[MAX_N * MAX_N << 1];

void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}

void addtube(int src, int dst, int weight)
{
    addpath(src, dst, weight);
    addpath(dst, src, 0);
}

bool bfs()
{
    memset(dep, 0, sizeof(dep));
    queue<int> q;
    dep[st] = 1, q.push(st);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (edges[i].weight > 0 && dep[edges[i].to] == 0)
                dep[edges[i].to] = dep[u] + 1, q.push(edges[i].to);
    }
    return dep[ed] != 0;
}

int dfs(int u, int flow)
{
    if (u == ed || flow == 0)
        return flow;
    for (int &i = cur[u]; i != -1; i = edges[i].nxt)
    {
        if (edges[i].weight > 0 && dep[edges[i].to] == dep[u] + 1)
        {
            int di = dfs(edges[i].to, min(flow, edges[i].weight));
            if (di)
            {
                edges[i].weight -= di, edges[i ^ 1].weight += di;
                return di;
            }
        }
    }
    return 0;
}

int dinic()
{
    int ans = 0;
    while (bfs())
    {
        memcpy(cur, head, sizeof(head));
        while (int di = dfs(st, INF))
            ans += di;
    }
    return ans;
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, x, y; i <= m; i++)
        scanf("%d%d", &x, &y), mp[x][y] = true;
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                mp[i][j] |= mp[i][k] && mp[k][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (mp[i][j])
                addtube(i, j + n, 1);
    st = 2 * n + 1, ed = st + 1;
    for (int i = 1; i <= n; i++)
        addtube(st, i, 1), addtube(i + n, ed, 1);
    printf("%d", n - dinic());
    return 0;
}
```

## C - The Painter

This problem is DP counting. We consider setting up a state: $dp[i][j]$ is the number of ways in which the first $i$ colors have all been painted and there are $j$ pairs of invalid adjacent colors. We consider first enumerating $i, j$, then enumerating the number of groups $x$ to split this color into, then enumerating $y$, the number of groups of this color to put in between the invalid ones to make them valid ($y \leq j$).

Then we consider the transition:

$$
dp[i + 1][j + c_{i + 1} - x - y] += dp[i][j] {c_{i + 1} - 1 \choose x - 1} {j \choose y} {(\sum_{w = 1}^i c_w) - y + 1 \choose x - y}
$$

Notes:

1. $j + c_{i + 1} - x - y$ is the invalid count after placement.
2. ${c_{i + 1} - 1 \choose x - 1}$ is the classic stars and bars.
3. ${j \choose y}$ is the number of ways to choose the invalid positions.
4. ${(\sum_{w = 1}^i c_w) - y + 1 \choose x - y}$ is the number of ways to choose some spots among the previous positions to place the remaining invalid groups.

```cpp
// C.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const ll MAX_K = 16, MAX_C = 7, MAX_N = MAX_K * MAX_C, mod = 1000000007;

ll T, n, k, ci[MAX_K], dp[MAX_N][MAX_N], sum[MAX_N], C[MAX_N][MAX_N];

ll comb(ll a, ll b) { return C[a][b]; }

signed main()
{
    C[0][0] = 1;
    for (int i = 1; i < MAX_N; i++)
    {
        C[i][0] = C[i][i] = 1;
        for (int j = 1; j <= i - 1; j++)
            C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
    }
    scanf("%lld", &T);
    while (T--)
    {
        scanf("%lld", &k), n = 0, sum[0] = 0;
        for (int i = 1; i <= k; i++)
            scanf("%lld", &ci[i]), n += ci[i], sum[i] = sum[i - 1] + ci[i];
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for (int i = 1; i <= k; i++)
            for (int j = 0; j <= sum[i - 1]; j++)
                if (dp[i - 1][j])
                    for (int x = 1; x <= ci[i]; x++)
                        for (int y = 0; y <= min(1LL * j, ci[i]); y++)
                            (dp[i][j + ci[i] - x - y] += dp[i - 1][j] * comb(ci[i] - 1, x - 1) % mod * comb(j, y) % mod * comb(sum[i - 1] - j + 1, x - y) % mod) %= mod;
        printf("%lld\n", dp[k][0]);
    }
    return 0;
}
```
