---
id: 1489
lang: en
translation_of: index.md
source_sha256: 006d72fc4e755852e5da52704905634394870088e2ea811431f0257afd41ab2e
title: "[Fortuna OJ] Jul 11th - Group B Problem-Solving Report"
description: "Solutions to three Group B problems: a DP sped up with a monotonic queue, a centroid decomposition problem, and a DP problem left unexplained."
tags: ["OI","Graph Theory","DP","Centroid Decomposition"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The ' - ' in the title and the three problem headings is kept as stored, matching the Chinese."
  - "卡指数 here means cutting the exponent of one's own solution (contrasted with a constant-factor optimization), not the glossary sense of setting limits to fail slower solutions; chipped as the fuller phrase 来卡指数 with its own gloss."
  - "数字八 (literally “the digit eight”) rendered as “Figure Eight”."
  - "原点 (origin) in problem B, apparently a slip for 零点 (zero point), is translated literally as “origin point”."
  - "The post ends mid-sentence with a comma after 懒得讲了; the English keeps the trailing comma."
---

## A - Photo

This problem really tests your thinking. Here I'll present the DP approach.

Let's consider DP. The equation can roughly be written as:

$$
dp[i] = max\{ dp[j], j \in S_i \} + 1
$$

Here, $dp[i]$ is the maximum number of spotted cows within $[1, i]$, and $S_i$ is the interval we can transition from. We can precompute, for every point, the corresponding $lft_{S_i}, rig_{S_i}$, that is, the left and right endpoints of each point's set. This interval satisfies one fundamental condition: it is the nearest set to the left of point $i$ that does not contain $i$. We can definitely get the **maximum** answer out of this interval. During preprocessing, first default to $rgt[i] = i - 1$, then just do a small DP at the end to update the data.

<!--more-->

Now let's think about optimizing this DP. After all, finding the range of $S_i$ is only a constant-factor optimization; now let's <span class="slang" data-zh="来卡指数" data-gloss="lái kǎ zhǐshù, “come jam the exponent”. Here: cutting down the exponent of one's own solution's complexity, rather than just its constant factor.">go after the exponent</span>. Notice that consecutive $\dots, S_{i-1}, S_i, S_{i+1}, \dots$ are **usually** (there are cases with no solution) contiguous too, so when we use the monotonic queue we can also maintain it contiguously. Watch out for the index distance at the head of the queue and the slope handling at the tail.

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

using namespace std;

const int MAX_N = 201000;

int dp[MAX_N], lft[MAX_N], rig[MAX_N], n, m, q[MAX_N << 1];

int main()
{
    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n + 1; i++)
        rig[i] = i - 1;

    for (int i = 1, l, r; i <= m; i++)
        scanf("%d%d", &l, &r), rig[r] = min(rig[r], l - 1), lft[r + 1] = max(lft[r + 1], l);
    for (int i = n; i >= 1; i--)
        rig[i] = min(rig[i], rig[i + 1]);
    for (int i = 2; i <= n + 1; i++)
        lft[i] = max(lft[i], lft[i - 1]);
    int l = 1, r = 1, j = 1;
    q[1] = 0;
    for (int i = 1; i <= n + 1; i++)
    {
        while (j <= min(rig[i], n))
        {
            if (dp[j] == -1)
            {
                j++;
                continue;
            }
            while (l <= r && dp[j] > dp[q[r]])
                r--;
            q[++r] = j, j++;
        }
        while (l <= r && q[l] < lft[i])
            l++;
        if (l <= r)
            dp[i] = dp[q[l]] + ((i != n + 1) ? 1 : 0);
        else
            dp[i] = -1;
    }
    printf("%d", dp[n + 1]);
    return 0;
}
```

## B - Yin and Yang

In the contest I did see that this was a <span class="slang" data-zh="sb" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span> centroid decomposition template problem, but my code <span class="slang" data-zh="挂" data-gloss="guà, “hang”. OI slang for a solution failing or losing points, usually because of a bug.">blew up</span>, which shows I'm still not fluent enough and need more practice.

When doing centroid decomposition, a chain starting from the root gives two pieces of information: the chain's weight sum, and whether it has a zero point. The problem requires us to check whether there is a zero point when recording a path. So when recording the answer, one thing is to special-case chains whose weight sum is zero, and another is to record, along with the chain weight, whether there is an origin point. Once these two things are clear, just do centroid decomposition.

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

using namespace std;

const int MAX_N = 101000;

int head[MAX_N], current, siz[MAX_N], n, dep[MAX_N], dist[MAX_N], mxdep;
int root_val, root, current_stat[MAX_N];
ll f[MAX_N << 1][2], g[MAX_N << 1][2], answer;
bool tag[MAX_N];

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

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

void __find_root(int u, int fa, int capacity)
{
    int mx_val = 0;
    siz[u] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa && !tag[edges[i].to])
            __find_root(edges[i].to, u, capacity), siz[u] += siz[edges[i].to], mx_val = max(mx_val, siz[edges[i].to]);
    mx_val = max(mx_val, capacity - siz[u]);
    if (mx_val < root_val)
        root_val = mx_val, root = u;
}

int find_root(int u, int capacity)
{
    root_val = capacity, root = 0;
    __find_root(u, 0, capacity);
    return root;
}

void dfs1(int u, int fa, int dep, int way)
{
    mxdep = max(mxdep, dep);
    if (current_stat[way] > 0)
        f[way][1]++;
    else
        f[way][0]++;
    current_stat[way]++;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa && !tag[edges[i].to])
            dfs1(edges[i].to, u, dep + 1, way + edges[i].weight);
    current_stat[way]--;
}

void dq(int u, int capacity)
{
    int mx = 0;
    tag[u] = true, g[MAX_N][0] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (!tag[edges[i].to])
        {
            mxdep = 1;
            dfs1(edges[i].to, u, 1, MAX_N + edges[i].weight);
            mx = max(mx, mxdep);
            answer += (g[MAX_N][0] - 1) * f[MAX_N][0];
            for (int d = -mxdep; d <= mxdep; d++)
                answer += (g[MAX_N - d][1] * f[MAX_N + d][1]) + (g[MAX_N - d][0] * f[MAX_N + d][1]) + (g[MAX_N - d][1] * f[MAX_N + d][0]);
            for (int d = MAX_N - mxdep; d <= MAX_N + mxdep; d++)
            {
                g[d][0] += f[d][0];
                g[d][1] += f[d][1];
                f[d][0] = f[d][1] = 0;
            }
        }
    for (int d = MAX_N - mx; d <= MAX_N + mx; d++)
        g[d][0] = g[d][1] = 0;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (!tag[edges[i].to])
        {
            int capacity_ = siz[edges[i].to];
            dq(find_root(edges[i].to, capacity_), capacity_);
        }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1, u, v, len; i <= n - 1; i++)
        scanf("%d%d%d", &u, &v, &len), addpath(u, v, len == 0 ? -1 : 1), addpath(v, u, len == 0 ? -1 : 1);
    dq(find_root(1, n), n);
    printf("%lld", answer);
    return 0;
}
```

## C - Figure Eight

This is just a DP problem. Too lazy to explain it,
