---
id: 1963
lang: en
translation_of: index.md
source_sha256: f79a040fbeb1860f9e673f6d0e36f0b634cb69985d4ee0da86e888324378a344
title: "[Fortuna OJ] Jan 12 Provincial Selection Division – Problem-Solving Report"
description: "However safe you feel, you’ll still lose points once the contest is over, and you’ll find a slight tweak can fucking add dozens of points. That’s life."
tags: ["OI","DFS","SAM","Small-to-Large Merging"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Title follows the English edition of post 1960: 省选组 rendered as “Provincial Selection Division”."
  - "Description translates the Chinese meta description, which is the post's epigraph."
  - "下表 read as a typo for 下标 (“indices”)."
  - "中心 rendered literally as “center” (the code finds the centroid)."
  - "Problem name 春节十二响 translated as “Twelve Chimes of the Spring Festival”, as in the English editions of posts 1092 and 1769."
---

> No matter how safe you feel, once the contest is over you’ll still have <span class="slang" data-zh="挂分" data-gloss="guà fēn, “hang points”. Losing points one should have had, usually to a bug.">lost points</span>, and you’ll find that a slight tweak can fucking add dozens of points. That’s life.
>
> kal0rona

<!--more-->

## A - Historical Itinerary

The official intended solution uses SA, which I don’t know, so I spent ages searching for SAM solutions and found a really nice approach.

Let’s build a SAM and maintain the Right sets with sets. Then DFS on the Link tree. Since the indices in each query are contiguous, for any one index in the set of tree node $u$, the contribution it can make is to update with its predecessor and successor within the subtree, that is, the two triples $(u_x, sub_1, dep_u)$ and $(sub_2, u_x, dep_u)$. Because if, when $u$ chooses from $sub$ and picks $y$, it picks a farther $z$ instead, then clearly the answer is no better than that of $(y, z)$, since the update was already done when the depth was larger. The sets can use small-to-large merging, done much like in “Twelve Chimes of the Spring Festival”, by swapping the IDs.

Then just sort + offline + sweep line to find the maximum point. Pretty interesting, actually.

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

using namespace std;

const int MAX_N = 2e6 + 200, bitnum = 17;
typedef unsigned long long ull;

int n, q, head[MAX_N], current, sid[MAX_N], tp_tot, ans[MAX_N];
char str[MAX_N];
set<int> st[MAX_N];

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

struct triple
{
    int x, y, d;
    bool operator<(const triple &rhs) const { return x < rhs.x; }
} tps[MAX_N << 1], queries[MAX_N];

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

void fileIO()
{
    freopen("history.in", "r", stdin);
    freopen("history.out", "w", stdout);
}

struct node
{
    int ch[2], link, dep;
} nodes[MAX_N];

int roots[MAX_N], atPos[MAX_N], ptot = 1, last_ptr = 1;
int fa[20][MAX_N], tree[MAX_N];

inline int lowbit(int x) { return x & (-x); }

void update(int x, int d)
{
    for (; x <= n; x += lowbit(x))
        tree[x] = max(tree[x], d);
}

int query(int x, int ret = 0)
{
    for (; x; x -= lowbit(x))
        ret = max(ret, tree[x]);
    return ret;
}

void insert(int c, int pos)
{
    int pre = last_ptr, p = last_ptr = ++ptot;
    nodes[p].dep = nodes[pre].dep + 1, st[p].insert(pos);
    while (pre && nodes[pre].ch[c] == 0)
        nodes[pre].ch[c] = p, pre = nodes[pre].link;
    if (pre == 0)
        nodes[p].link = 1;
    else
    {
        int q = nodes[pre].ch[c];
        if (nodes[q].dep == nodes[pre].dep + 1)
            nodes[p].link = q;
        else
        {
            int clone = ++ptot;
            nodes[clone] = nodes[q], nodes[clone].dep = nodes[pre].dep + 1;
            nodes[q].link = nodes[p].link = clone;
            while (pre && nodes[pre].ch[c] == q)
                nodes[pre].ch[c] = clone, pre = nodes[pre].link;
        }
    }
}

void dfs(int u)
{
    for (int i = head[u]; i != -1; i = edges[i].nxt)
    {
        dfs(edges[i].to);
        if (st[sid[edges[i].to]].size() > st[sid[u]].size())
            swap(sid[edges[i].to], sid[u]);
        for (set<int>::iterator it = st[sid[edges[i].to]].begin(); it != st[sid[edges[i].to]].end(); it++)
        {
            set<int>::iterator tmp = st[sid[u]].upper_bound(*it);
            if (tmp != st[sid[u]].end())
                tps[++tp_tot] = triple{*it, *tmp, nodes[u].dep};
            if (tmp != st[sid[u]].begin())
                tps[++tp_tot] = triple{*(--tmp), *it, nodes[u].dep};
            st[sid[u]].insert(*it);
        }
    }
}

int main()
{
    memset(head, -1, sizeof(head));
    // fileIO();
    scanf("%d%d%s", &n, &q, str + 1);
    for (int i = 1; i <= n; i++)
        insert(str[i] - '0', i);
    sid[1] = 1;
    for (int i = 2; i <= ptot; i++)
        addpath(nodes[i].link, i), sid[i] = i;
    dfs(1);
    for (int i = 1; i <= q; i++)
        scanf("%d%d", &queries[i].x, &queries[i].y), queries[i].d = i;
    sort(tps + 1, tps + 1 + tp_tot), sort(queries + 1, queries + 1 + q);
    int tp_ptr = tp_tot;
    for (int q_ptr = q; q_ptr >= 1; q_ptr--)
    {
        while (tp_ptr >= 1 && tps[tp_ptr].x >= queries[q_ptr].x)
            update(tps[tp_ptr].y, tps[tp_ptr].d), tp_ptr--;
        ans[queries[q_ptr].d] = query(queries[q_ptr].y);
    }
    for (int i = 1; i <= q; i++)
        printf("%d\n", ans[i]);
    return 0;
}
```

## B - Flea Kingdom

First find the center and make it the root, then put the root’s children into a vector and call them “candidate cut vertices”. We sort the “candidate cut vertices” by subtree size from largest to smallest and call the first $x$ of them “cut vertices”, where the sum of these vertices’ subtree sizes is just greater than or equal to $\frac{n}{2}$.

Now let’s do several DFS passes to get the answers. First we enumerate the cut vertices and DFS with each one as the subtree root; each vertex’s answer is then $x$ or $x-1$. How do we decide whether to subtract one? We just need to check whether the upward tree edge of the cut-vertex root is necessary.

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

using namespace std;

const int MAX_N = 1e6 + 200;

int head[MAX_N], current, n, siz[MAX_N], root, answer[MAX_N], acc, limit_id;
vector<int> vec;

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

void fileIO()
{
    freopen("flea.in", "r", stdin);
    freopen("flea.out", "w", stdout);
}

bool compare(int a, int b) { return siz[a] > siz[b]; }

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

void dfs(int u, int fa)
{
    int max_son = 0;
    siz[u] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            dfs(edges[i].to, u), siz[u] += siz[edges[i].to], max_son = max(max_son, siz[edges[i].to]);
    max_son = max(max_son, n - siz[u]);
    if (max_son <= (n >> 1))
        root = u;
}

void getAns(int u, int fa)
{
    answer[u] = limit_id - (siz[u] + acc >= (n >> 1)) + 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            getAns(edges[i].to, u);
}

int main()
{
    // fileIO();
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1, u, v; i <= n - 1; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    dfs(1, 0), dfs(root, 0);
    for (int i = head[root]; i != -1; i = edges[i].nxt)
        vec.push_back(edges[i].to);
    sort(vec.begin(), vec.end(), compare);
    limit_id = -1;
    for (int i = 0, siz_ = vec.size(); i < siz_; i++)
    {
        acc += siz[vec[i]];
        if (acc >= (n >> 1))
        {
            limit_id = i;
            break;
        }
    }
    for (int i = 0; i <= limit_id; i++)
        acc -= siz[vec[i]], getAns(vec[i], root), acc += siz[vec[i]];
    if (limit_id != -1)
        acc -= siz[vec[limit_id]];
    for (int i = limit_id + 1, siz_ = vec.size(); i < siz_; i++)
        getAns(vec[i], root);
    for (int i = 1; i <= n; i++)
        printf("%d\n", answer[i]);
    return 0;
}
```

## C - Wall

First, we add the edges $i \to p_i$ and get a graph with many cycles. Analyzing the statement, we find that the problem asks us to delete some edges, leaving $\frac{n}{2}$ single edges, with no two edges crossing. So for cycles of length $2$, we make the smaller-numbered vertex a left parenthesis and the larger-numbered one a right parenthesis; for cycles of even length, we find there are also only two choices, so just search directly.

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

using namespace std;

const int MAX_N = 110;
typedef long long ll;
typedef pair<int, ll> pil;

int n, pi[MAX_N], deg[MAX_N], seq[MAX_N];

void fileIO()
{
    freopen("wall.in", "r", stdin);
    freopen("wall.out", "w", stdout);
}

bool dfs(int pos, int acc)
{
    if (acc < 0)
        return false;
    if (pos == n + 1)
        return true;
    if (seq[pos] != 0)
        return dfs(pos + 1, acc + seq[pos]);
    int x = pos, len = 0, sign = 1;
    while (len == 0 || x != pos)
        seq[x] = sign, x = pi[x], len++, sign = -sign;
    if (dfs(pos + 1, acc + seq[pos]))
        return true;
    x = pos, len = 0, sign = -1;
    while (len == 0 || x != pos)
        seq[x] = sign, x = pi[x], len++, sign = -sign;
    if (dfs(pos + 1, acc + seq[pos]))
        return true;
    seq[pos] = 0;
    return false;
}

int main()
{
    // fileIO();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &pi[i]);
    for (int i = 1; i <= n; i++)
        if (pi[i] > i && i == pi[pi[i]])
            seq[i] = 1, seq[pi[i]] = -1;
    if (dfs(1, 0))
        for (int i = 1; i <= n; i++)
            if (seq[i] == 1)
                putchar('(');
            else
                putchar(')');
    return 0;
}
```
