题目 › usaco-1495

Vocabulary Quiz

USACO 2025 February · 银组 · 2025-02 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Bessie is helping Elsie with her upcoming vocabulary quiz. The words to be tested will be drawn from a bank of $M$ distinct words, where no word in the bank is a prefix of another word in the bank.

While the bank is nonempty, Bessie will select a word from the bank, remove it from the bank, and read it to Elsie one character at a time from left to right. Elsie's task is to tell Bessie once she can uniquely identify it, after which Bessie will stop reading.

Bessie has already decided to read the words from the word bank in the order $w_1,w_2,\dots,w_M$. If Elsie answers as quickly as possible, how many characters of each word will Bessie read?

The words are given in a compressed format. We will first define $N+1$ ($1\le N\le 10^6$) distinct words, and then the word bank will consist of all those words that are not a prefix of another word. The words are defined as follows:

  • Initially, the $0$th word will be the empty string.
  • Then for each $1\le i\le N$, the $i$th word will be equal to the $p_i$th word plus an additional character at the end ($0\le p_i<i$). The characters are chosen such that all $N+1$ words are distinct.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$, where $N+1$ is the number of words given in the compressed format.

The next line contains $p_1,p_2,\dots,p_N$ where $p_i$ represents that the $i$-th word is formed by taking the $p_i$-th word and adding a single character to the end.

Let $M$ be the number of words that are not a prefix of some other word. The next $M$ lines will contain $w_1,w_2,\dots,w_M$, meaning that the $w_i$th word will be the $i$th to be read. It is guaranteed that the words to be read form a permutation of the words in the word bank.

OUTPUT FORMAT (print output to the terminal / stdout):

Output $M$ lines, where the $i$th line contains the number of characters of the $i$th word that Bessie reads.

SAMPLE INPUT:

5
0 1 2 3 4
5

SAMPLE OUTPUT:

0

There are $6$ words labeled $0\dots 5$. Word $5$ is the only one that is not a prefix of another word, so it is the only one in the bank. In general, once only one word is left in the bank, Elsie won't need any characters to identify it.

SAMPLE INPUT:

4
0 0 1 1
4
3
2

SAMPLE OUTPUT:

2
1
0

The bank consists of words $2$, $3$, and $4$.

Elsie needs two characters to identify word $4$ since words $3$ and $4$ share their first character in common.

Once Bessie reads the first character of word $3$, Elsie has enough characters to uniquely identify it, since word $4$ was already read.

SAMPLE INPUT:

4
0 0 1 1
2
3
4

SAMPLE OUTPUT:

1
2
0

SCORING:

  • Inputs 4-5: No word has length greater than $20$.
  • Inputs 6-10: The sum of the lengths of all words in the word bank does not exceed $10^7$.
  • Inputs 11-18: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 29.1k平均输出 token(含思考) ↓
  • 25.8输出速度 tokens/s ↑
  • 1126 秒延迟 ↓
  • 1.22×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 5Pylint 问题 ↓
  • 9.50Pylint 得分 ↑
  • 24圈复杂度 ↓
  • 50.7可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 23.9k平均输出 token(含思考) ↓
  • 206.5输出速度 tokens/s ↑
  • 117 秒延迟 ↓
  • 0.97×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 6Pylint 问题 ↓
  • 9.30Pylint 得分 ↑
  • 23圈复杂度 ↓
  • 49.7可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 16.5k平均输出 token(含思考) ↓
  • 127.1输出速度 tokens/s ↑
  • 131 秒延迟 ↓
  • 0.43×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 9.19Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 71.0可维护性指数 ↑

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 140 行
import sys
from array import array


def solve():
    tokens = sys.stdin.buffer.read().split()
    if not tokens:
        return

    n = int(tokens[0])
    size = n + 1

    parent = array('i', [0]) * size
    depth = array('i', [0]) * size
    first_child = array('i', [-1]) * size
    next_sibling = array('i', [-1]) * size

    for i in range(1, size):
        p = int(tokens[i])
        parent[i] = p
        depth[i] = depth[p] + 1
        next_sibling[i] = first_child[p]
        first_child[p] = i

    m = 0
    for x in first_child:
        if x == -1:
            m += 1

    read_order = array('i', [0]) * m
    base = n + 1
    for i in range(m):
        read_order[i] = int(tokens[base + i])
    del tokens

    if m == 1:
        sys.stdout.write("0\n")
        return

    # Produce a DFS preorder and the corresponding order of leaves.
    preorder = array('i')
    leaves = array('i')
    stack = [0]

    while stack:
        u = stack.pop()
        preorder.append(u)

        c = first_child[u]
        if c == -1:
            # Reuse first_child as the leaf-rank array.
            first_child[u] = len(leaves)
            leaves.append(u)
        else:
            while c != -1:
                stack.append(c)
                c = next_sibling[c]
            first_child[u] = -1

    del stack, next_sibling

    prev = array('i', [-1]) * m
    nxt = array('i', [-1]) * m
    for i in range(m):
        prev[i] = i - 1
        if i + 1 < m:
            nxt[i] = i + 1

    # Only the nearest remaining leaves on either side in DFS order can
    # determine the deepest LCA with the current leaf.
    query_head = array('i', [-1]) * size
    query_next = array('i')
    query_other = array('i')
    query_step = array('i')
    best = array('i', [-1]) * m

    for step in range(m):
        v = read_order[step]
        r = first_child[v]
        a = prev[r]
        b = nxt[r]

        if a != -1:
            # leaves[a] occurs before v in preorder.
            u = leaves[a]
            q = len(query_next)
            query_next.append(query_head[u])
            query_head[u] = q
            query_other.append(v)
            query_step.append(step)

        if b != -1:
            # v occurs before leaves[b] in preorder.
            u = leaves[b]
            q = len(query_next)
            query_next.append(query_head[v])
            query_head[v] = q
            query_other.append(u)
            query_step.append(step)

        if a != -1:
            nxt[a] = b
        if b != -1:
            prev[b] = a

    del read_order, leaves, prev, nxt, first_child

    # Offline LCA queries. Reversed preorder is a bottom-up traversal.
    # Each query is stored at its later endpoint in this traversal.
    dsu = array('i', range(size))

    for u in reversed(preorder):
        q = query_head[u]
        while q != -1:
            x = query_other[q]

            # Find with path halving. With these directed unions, the
            # resulting root is exactly the LCA.
            while dsu[x] != x:
                dsu[x] = dsu[dsu[x]]
                x = dsu[x]

            d = depth[x]
            s = query_step[q]
            if d > best[s]:
                best[s] = d

            q = query_next[q]

        if u != 0:
            dsu[u] = parent[u]

    del dsu, query_head, query_next, query_other, query_step
    del preorder, parent, depth

    answer = ['0' if d == -1 else str(d + 1) for d in best]
    sys.stdout.write('\n'.join(answer) + '\n')


solve()