QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#7320. 質數樹

Statistics

Bobo 提出了一種有根樹的乘法運算。

設 $A$ 和 $B$ 是任意兩棵有根樹。對每個頂點 $x\in A$,建立一份 $B$ 的副本,並將這份副本的根與 $x$ 合併,便得到 $T=A\cdot B$(詳情請見下圖)。我們稱 $A$ 和 $B$ 為 $T$ 的因子。

problem_7320_bbdddb19ff78d0dce1717328588db7c5.png

顯然,$T\cdot\mathbf{1}=\mathbf{1}\cdot T=T$,其中 $\mathbf{1}$ 是只有一個頂點的有根樹。因此,$\mathbf{1}$ 是每棵有根樹的因子,而每棵有根樹也都是自身的因子。若一棵有根樹 $T$ 的因子只有 $T$ 和 $\mathbf{1}$,我們就稱 $T$ 為質數樹。

Bobo 有一棵包含 $n$ 個節點的有根樹 $T$,為方便起見,節點編號為 $1,2,\ldots,n$。他想將 $T$ 分解成盡可能多棵質數樹的乘積(也就是找出等式 $T=T_1\cdot T_2\cdots T_m$,其中 $T_i$($1\le i\le m$)都是質數樹,並使 $m$ 最大)。

注意,$\mathbf{1}$ 不是質數樹。

輸入格式

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

第一行包含一個整數 $n$,表示節點數量($2\le n\le10^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.