题面
来源: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
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
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
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
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()
import sys from array import array from bisect import bisect_left def main() -> None: data = sys.stdin.buffer.read() L = len(data) idx = 0 # Read N while idx < L and data[idx] <= 32: idx += 1 n = 0 while idx < L: c = data[idx] if c <= 32: break n = n * 10 + (c - 48) idx += 1 size = n + 1 parent = array('i', [0]) * size head = array('i', [-1]) * size nxt = array('i', [-1]) * size # Build the tree for i in range(1, n + 1): while idx < L and data[idx] <= 32: idx += 1 p = 0 while idx < L: c = data[idx] if c <= 32: break p = p * 10 + (c - 48) idx += 1 parent[i] = p nxt[i] = head[p] head[p] = i # f[v] will become the maximum read-time among leaves in v's subtree. f = array('i', [-1]) * size order = array('i') t = 0 while idx < L: while idx < L and data[idx] <= 32: idx += 1 if idx >= L: break x = 0 while idx < L: c = data[idx] if c <= 32: break x = x * 10 + (c - 48) idx += 1 order.append(x) f[x] = t t += 1 del data # Bottom-up max-time propagation. for i in range(n, 0, -1): fi = f[i] p = parent[i] if fi > f[p]: f[p] = fi del parent ans = array('i', [0]) * size if head[0] == -1: ans[0] = 0 else: # path_neg contains -f[v] for nodes on the current root-to-node path. # Since f values are non-increasing down the tree, path_neg is sorted. path_neg = [-f[0]] stack_v = [0] stack_e = [head[0]] while stack_v: e = stack_e[-1] if e == -1: stack_v.pop() stack_e.pop() path_neg.pop() continue stack_e[-1] = nxt[e] path_neg.append(-f[e]) if head[e] == -1: # Leaf: number of ancestors with f > f[e] is the answer. ans[e] = bisect_left(path_neg, -f[e]) path_neg.pop() else: stack_v.append(e) stack_e.append(head[e]) del head, nxt, f out = bytearray() for x in order: out.extend(str(ans[x]).encode()) out.append(10) sys.stdout.buffer.write(out) if __name__ == "__main__": main()
import sys from array import array def solve(): read = sys.stdin.buffer.readline write = sys.stdout.write n = int(read()) # parent[0] = 0 for the empty word. parent = array('i', [0]) while len(parent) < n + 1: parent.extend(map(int, read().split())) has_child = bytearray(n + 1) depth = array('i', [0]) * (n + 1) for v in range(1, n + 1): p = parent[v] has_child[p] = 1 depth[v] = depth[p] + 1 # Leaves are exactly the words in the bank. m = n + 1 - sum(has_child) # Initially, leaves store their reading positions. # Internal nodes will be filled by bottom-up propagation. latest = array('i', [0]) * (n + 1) for position in range(1, m + 1): leaf = int(read()) latest[leaf] = position # Since parent[v] < v, decreasing index order is bottom-up. for v in range(n, 0, -1): p = parent[v] value = latest[v] if value > latest[p]: latest[p] = value answer = array('i', [0]) * m # A differing subtree maximum identifies the shallowest valid prefix # for the word read at position latest[v]. for v in range(1, n + 1): p = parent[v] position = latest[v] if position != latest[p]: answer[position - 1] = depth[v] # The final word's answer remains 0 because the root distinguishes it. chunk_size = 32768 for start in range(0, m, chunk_size): write('\n'.join(map(str, answer[start:start + chunk_size]))) write('\n') if __name__ == "__main__": solve()