QOJ.ac

QOJ

Süre Sınırı: 2.0 s Bellek Sınırı: 512 MB Toplam puan: 100 Hack'lenebilir ✓

#7320. 質數樹

İstatistikler

Bobo 提出咗一種有根樹之間嘅乘法運算。

設 $A$ 同 $B$ 係任意兩棵有根樹。要建立 $T = A \cdot B$,就對每個頂點 $x \in A$ 複製一份 $B$,再將呢份副本嘅根同 $x$ 合併(詳情請睇下圖)。我哋將 $A$ 同 $B$ 叫做 $T$ 嘅因子。

problem_7320_bfdc9266411a25e1c7fef51a3c57ff0f.png

顯然,$T \cdot 1 = 1 \cdot T = T$,其中 $1$ 係只有一個頂點嘅有根樹。所以,$1$ 係每棵有根樹嘅因子,而每棵有根樹亦都係自己嘅因子。如果一棵有根樹 $T$ 只有 $T$ 同 $1$ 呢兩個因子,我哋就將 $T$ 叫做質數樹。

Bobo 有一棵包含 $n$ 個節點嘅有根樹 $T$,節點為咗方便而編號為 $1, 2, \ldots, n$。佢想將 $T$ 分解成盡可能多棵質數樹嘅乘積(即係搵出等式 $T = T_1 \cdot T_2 \cdot \ldots \cdot T_m$,其中 $T_i$($1 \le i \le m$)都係質數樹,而且 $m$ 盡可能大)。

留意,$1$ 唔係質數樹。

輸入格式

輸入包含零組或多組測試資料,以檔案結尾結束。對每組測試資料:

第一行包含一個整數 $n$,表示節點數目($2 \le n \le 10^6$)。

第二行包含 $(n - 1)$ 個整數 $p_2, p_3, \ldots, p_n$,其中 $p_i$ 係第 $i$ 個節點嘅父節點($1 \le p_i \le i - 1$)。

保證所有 $n$ 嘅總和唔超過 $10^6$。

輸出格式

對每組測試資料,輸出一個整數,表示質因子數目嘅最大值。

樣例

輸入格式 1

12
1 1 1 1 2 2 4 5 5 6 10
3
1 1
6
1 1 1 2 3
13
1 1 1 2 2 3 3 4 5 6 7 8

輸出格式 1

3
1
2
1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.