QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 512 MB مجموع النقاط: 100

#18306. 完美二元樹

الإحصائيات

完美二元樹(perfect binary tree)是指一棵有根樹,其中每個非葉節點恰好有兩個子節點,且所有葉節點與根節點的距離皆相等。

無根完美二元樹(unrooted perfect binary tree)是指一棵無根樹,當將其任一節點視為根時,它會成為一棵完美二元樹。

Bessie 擁有一棵包含 $N$ ($1 \le N \le 10^5$) 個節點的樹。請計算有多少種移除邊的子集的方法,使得剩下的森林是由多棵無根完美二元樹所組成的。由於答案可能非常大,請輸出對 $10^9+7$ 取模後的結果。

輸入格式

第一行包含一個整數 $T$ ($1 \leq T \leq 100$),代表獨立測試案例的數量。

每個測試案例的第一行包含一個整數 $N$。

接下來的 $N-1$ 行,每行包含兩個整數 $u_i$ 和 $v_i$ ($1 \leq u_i, v_i \leq N$),表示節點 $u_i$ 和 $v_i$ 之間有一條邊。

保證每個測試案例中,給定的邊構成一棵包含 $N$ 個節點的樹。

此外,所有測試案例的 $N$ 之總和不超過 $2\cdot 10^5$。

輸出格式

對於每個測試案例,輸出一個整數:移除邊後形成無根完美二元樹森林的方法數,對 $10^9+7$ 取模。

範例

輸入 1

3
6
1 2
3 2
4 6
5 6
6 2
3
1 2
3 2
7
2 1
2 3
1 6
1 7
3 4
3 5

輸出 1

8
2
14

說明

在第一個測試案例中,Bessie 可以移除以下任一邊的子集來獲得完美二元樹森林:

  1. $(2, 6)$
  2. $(1, 2)$, $(2, 3)$, $(2, 6)$
  3. $(1, 2)$, $(2, 3)$, $(4, 6)$
  4. $(1, 2)$, $(2, 3)$, $(5, 6)$
  5. $(1, 2)$, $(4, 6)$, $(5, 6)$
  6. $(2, 6)$, $(4, 6)$, $(5, 6)$
  7. $(2, 3)$, $(4, 6)$, $(5, 6)$
  8. $(1, 2)$, $(2, 3)$, $(2, 6)$, $(4, 6)$, $(5, 6)$

第一個子集產生兩棵高度為 $1$ 的子樹,最後一個子集產生六棵高度為 $0$ 的子樹,其餘子集則產生三棵高度為 $0$ 的子樹與一棵高度為 $1$ 的子樹。

子任務

  • 測試點 2-3:$N\le 15$
  • 測試點 4-5:沒有節點與超過兩個其他節點相鄰。
  • 測試點 6-9:$N\le 1000$,所有 $N$ 之總和不超過 $2000$,且沒有節點與超過三個其他節點相鄰。
  • 測試點 10-13:沒有節點與超過三個其他節點相鄰。
  • 測試點 14-21:無額外限制。

題目提供者:Avnith Vijayram

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.