QOJ.ac

QOJ

Time Limit: 10 s Memory Limit: 512 MB Total points: 100 Difficulty: [show] Hackable ✓

#1222. 多邊形

Statistics

久莲係一個鍾意出題嘅女仔。

今年嘅 World Final 完咗之後,久莲特別鍾意計算幾何,所以佢打算喺 NOI 考場都出一條計算幾何題:呢條題目只有個名同計算幾何有關。

首先,久莲畀咗一棵有 $n$($n\ge2$)個節點嘅有根樹 $T$,根節點編號係 $1$。定義葉節點為根以外所有度數啱啱係 $1$ 嘅節點。下圖係樹 $T$ 嘅一個例子,葉節點集合係 $\{3,4,5\}$。

problem_1222_08e46447caf5811ae94677cc5cc67493.png

之後,久莲用呢棵樹構造咗一個序列 $A$:

  • 由根節點開始,對成棵樹做深度優先遍歷,遍歷嗰陣按編號由細到大嘅順序訪問子節點,咁就可以得到樹 $T$ 嘅 DFS 序。
  • 之後,久莲按葉節點喺 DFS 序出現嘅先後順序,由前到後排成一行,得到序列 $A$。

再進一步,久莲透過序列 $A$ 定義咗兩個葉節點 $u,v$ 嘅距離 $d(u,v)$:假設 $u$ 係 $A$ 嘅第 $i$ 個元素,$v$ 係第 $j$ 個元素,咁 $d(u,v)=\min(|i-j|,|A|-|i-j|)$。其中 $|A|$ 係序列長度,即係 $T$ 嘅葉節點數量;$i,j$ 指出現位置,由 $1$ 開始數。

喺上面嘅例子,序列 $A$ 係 $[4,5,3]$,其中 $d(3,5)=d(3,4)=d(4,5)=1$,$3,4,5$ 嘅出現位置分別係 $3,1,2$。

最後,久莲畀咗一個參數 $K$。用呢棵有根樹 $T$ 同序列 $A$,我哋可以構造一張有 $n$ 個點、冇重邊亦冇自環嘅無向圖 $G$。兩個唔同嘅點 $u,v$ 之間有邊,當且僅當佢哋滿足以下至少一個條件:

  • 樹 $T$ 入面有一條邊連接 $u,v$。
  • 喺樹 $T$ 入面,$u,v$ 都係葉節點,而且 $d(u,v)\le K$。

當 $K=1$ 或 $2$,上面例子得到嘅圖 $G$ 都如下圖所示:

而家久莲想你計吓 $G$ 入面有幾多條唔同嘅哈密爾頓迴路。答案可能好大,請對 $998244353$ 取模再輸出。

problem_1222_f26b4b2a8848a3da9ef14f4b51ec0fa7.png

以下係一啲補充定義:

  • 冇重邊亦冇自環嘅無向圖 $G$ 嘅一條哈密爾頓迴路 $H$,係 $G$ 嘅邊嘅一個子集;其中每個點啱啱有兩條唔同嘅相鄰邊喺 $H$ 入面,而且任何兩個點都可以經 $H$ 入面嘅邊直接或者間接到達。
  • 冇重邊亦冇自環嘅無向圖 $G$ 嘅兩條哈密爾頓迴路 $H_1,H_2$ 唔同,當且僅當有一條邊 $e$ 喺 $H_1$ 入面但唔喺 $H_2$ 入面。

輸入格式

由檔案 polygon.in 讀入資料。

第一行輸入兩個整數 $n,K$,表示樹 $T$ 嘅點數同久莲揀定嘅參數 $K$。

第二行輸入 $n-1$ 個整數 $f_i$($1\le f_i\le i$),其中 $f_i$ 表示樹 $T$ 入面有邊 $(f_i,i+1)$。

輸出格式

輸出到檔案 polygon.out。

輸出一行一個整數,表示哈密爾頓迴路數量對 $998244353$ 取模嘅結果。

範例

輸入 1

5 1
1 1 2 2

輸出 1

2

說明

呢個範例同題面入面嘅例子完全一樣。兩條哈密爾頓迴路經過節點嘅順序分別係 $(1,2,4,5,3)$ 同 $(1,2,5,4,3)$。

子任務

各個測試點嘅資料規模同性質如下表:

編號 $n$ $K$ 特殊性質 編號 $n$ $K$ 特殊性質
$1$ $\le5$ $\le3$ 冇 $11$ $\le1000$ $\le2$ A
$2$ $\le10$ $\le3$ 冇 $12$ $\le1000$ $\le2$ A
$3$ $\le15$ $\le3$ 冇 $13$ $\le1000$ $\le2$ A
$4$ $\le20$ $\le3$ 冇 $14$ $\le1000$ $\le2$ 冇
$5$ $\le1000$ $=1$ A $15$ $\le1000$ $\le2$ 冇
$6$ $\le1000$ $=1$ A $16$ $\le1000$ $\le2$ 冇
$7$ $\le1000$ $=1$ A $17$ $\le1000$ $\le3$ A
$8$ $\le1000$ $=1$ 冇 $18$ $\le1000$ $\le3$ A
$9$ $\le1000$ $=1$ 冇 $19$ $\le1000$ $\le3$ 冇
$10$ $\le1000$ $=1$ 冇 $20$ $\le1000$ $\le3$ 冇

其中特殊性質 A 係保證樹上所有節點最多有兩個子節點。

對所有資料,保證 $1\le f_i\le i$、$2\le n\le1000$。

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.