---
id: 1794
lang: en
translation_of: index.md
source_sha256: 70c6bccfecd97454b6b790ce92d613ac7662777549a97420d430f33b531c4b87
title: "Nowcoder CSP-S Senior Division Pre-Contest Training Camp 1 - Problem-Solving Report"
description: "Solutions to Nowcoder CSP-S Senior Division Pre-Contest Training Camp 1: a first-player-win observation for A, rerooting DP for B, bipartite matching for C."
tags: ["OI","Problem-Solving Report","Nowcoder"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem names translated: 仓鼠的石子游戏 The Hamster's Stone Game, 乃爱与城市拥挤程度 Nai'ai and City Congestion (乃爱 is a character name, romanized), 小w的魔术扑克 Little w's Magic Poker; 牛客 rendered as Nowcoder, 提高组 as Senior Division, 赛前集训营 as Pre-Contest Training Camp."
  - "见图方式 in problem C read as a typo for 建图方式 (how the graph is built)."
  - "上次到的左边 rendered literally as “the left end reached last time”."
---

## A - The Hamster's Stone Game

<span class="slang" data-zh="诶嘿题" data-gloss="ēihēi tí, “heh-heh problem”. 诶嘿 is a pleased little giggle; a problem with a cute trick that makes you grin once you spot it.">A heh-heh problem</span>. After drawing a few diagrams I found that only the $1$ case is a first-player win, so just consider swapping the first and second player over multiple rounds.

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

using namespace std;

const int MAX_N = 1e3 + 200, table[8] = {0, 0, 1, 1, 1, 1, 1, 1};

int T, n, ai[MAX_N];

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &ai[i]);
        bool flag = false;
        for (int i = 1; i <= n; i++)
            flag ^= (ai[i] == 1);
        printf(flag ? "rabbit\n" : "hamster\n");
    }
    return 0;
}
```

<!--more-->

## B - Nai'ai and City Congestion

$\Theta(n^2)$ is easy, so I won't go into it.

Consider rerooting DP: move the parent's information over to the son, then restore it afterward. But it's so <span class="slang" data-zh="特么" data-gloss="tème, a euphemistic spelling of 他妈 (tāmā), “his mother”. A mild curse, like “damn” or “freaking”.">damn</span> hard to write, so I'd still recommend everyone write it with Up & Down (and that way of writing it really does look nicer, too). This is my 80-point code from the contest:

```cpp
// B.cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)

using namespace std;

const int MAX_N = 1e5 + 200, MAX_K = 15, mod = 1e9 + 7;

int head[MAX_N], current, n, k, dp[MAX_N][MAX_K], prod[MAX_N][MAX_K];
int ans[MAX_N], ans2[MAX_N], org[MAX_N][MAX_K];

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

namespace IO
{
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc()                                                                 \
    (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
         ? EOF                                                               \
         : *p1++)
inline int rd()
{
    int x = 0, f = 1;
    char c = gc();
    while (!isdigit(c))
    {
        if (c == '-')
            f = -1;
        c = gc();
    }
    while (isdigit(c))
        x = x * 10 + (c ^ 48), c = gc();
    return x * f;
}
} // namespace IO

int quick_pow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

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

void build_prod(int u, int fa)
{
    prod[u][0] = dp[u][0] % mod;
    for (int i = 1; i <= k; i++)
        prod[u][i] = (1LL * prod[u][i - 1] + 1LL * dp[u][i]) % mod;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            for (int j = 1; j <= k; j++)
                prod[u][j] = (1LL * prod[u][j] * prod[edges[i].to][j - 1] % mod);
}

void dfs(int u, int fa)
{
    dp[u][0] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            dfs(edges[i].to, u);
            for (int j = 1; j <= k; j++)
                dp[u][j] += dp[edges[i].to][j - 1];
        }
    // get the prod;
    build_prod(u, fa);
}

void redfs(int u, int fa)
{
    // collecting data;
    for (int i = 0; i <= k; i++)
        ans[u] += dp[u][i];
    build_prod(u, 0);
    ans2[u] = prod[u][k];
    // to change the root;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            // make things ready for the subtree;
            for (int j = 1; j <= k; j++)
                dp[u][j] -= dp[edges[i].to][j - 1];
            for (int j = 1; j <= k; j++)
                dp[edges[i].to][j] += dp[u][j - 1];

            build_prod(u, edges[i].to);

            redfs(edges[i].to, u);

            for (int j = 1; j <= k; j++)
                dp[edges[i].to][j] -= dp[u][j - 1];
            for (int j = 1; j <= k; j++)
                dp[u][j] += dp[edges[i].to][j - 1];

            build_prod(edges[i].to, u);
            build_prod(u, 0);
            // rec on myself;
            for (int j = 1; j <= k; j++)
                prod[u][j] = 1LL * prod[u][j] * prod[edges[i].to][j - 1] % mod;
        }
}

int main()
{
    memset(head, -1, sizeof(head));
    n = IO::rd(), k = IO::rd();
    for (int i = 1, u, v; i <= n - 1; i++)
        u = IO::rd(), v = IO::rd(), addpath(u, v), addpath(v, u);
    dfs(1, 0), redfs(1, 0);
    for (int i = 1; i <= n; i++)
        printf("%d ", ans[i]);
    puts("");
    for (int i = 1; i <= n; i++)
        printf("%d ", ans2[i]);
    puts("");
    return 0;
}
```

## C - Little w's Magic Poker

Looks like I need to properly review bipartite graphs.

We can build a bipartite graph, with the card faces on the left side and the card indices on the right. For how the graph is built, see the code: building it this way uses a property, namely that straights are all one big maximal connected block, and every straight is a subset of a maximal connected block; running the Hungarian algorithm from the right endpoint matches the largest part that can be matched, and then we find that if it can't be matched, the left end reached last time has to be given up: because it has been cut off from the part it could connect to before.

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

using namespace std;

const int MAX_N = 2e5 + 200;

int head[MAX_N], current, n, k, q, vis[MAX_N], pre[MAX_N], lft[MAX_N], ncnt, matching[MAX_N];

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

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

bool hungery(int u, int org)
{
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (vis[edges[i].to] != org)
        {
            vis[edges[i].to] = org;
            if (!matching[edges[i].to] || hungery(matching[edges[i].to], org))
            {
                matching[edges[i].to] = u, pre[u] = edges[i].to;
                return true;
            }
        }
    return false;
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &k);
    for (int i = 1, u, v; i <= k; i++)
    {
        scanf("%d%d", &u, &v);
        addpath(u, i + n), addpath(i + n, u);
        addpath(v, i + n), addpath(i + n, v);
    }
    int lcur = 1;
    for (int i = 1; i <= n; i++)
    {
        // fine the leftmost point to pair a conti;
        while (!hungery(i, ++ncnt) && lcur <= i)
            matching[pre[lcur]] = false, lcur++;
        lft[i] = lcur;
    }
    scanf("%d", &q);
    while (q--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        if (lft[r] <= l)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
```
