QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 1024 MB Puntuación total: 100

#4716. 行走

Estadísticas

一天早上,Răzvăran 决定邀请他的两位朋友 Matthew 和 Peter 去一个著名的公园散步。这个公园的形状是一棵有 $n$ 个节点(编号从 $1$ 到 $n$)的树,节点之间有 $n-1$ 条路径,每条路径长 $10$ 米。在每个节点 $i$ 处都有一个编号为 $i$ 的泉水。朋友们只有在散步满足以下规则时才会接受邀请:Matthew 是一个自命不凡的年轻人,他想要以尽可能短的距离走遍所有的泉水;而 Peter 想要挑战自我,他说他想按预定的顺序访问 $m$ 个泉水:$P_1, P_2, \dots, P_m$。Răzvăran 现在想知道他们有多少种散步方式,已知起点和终点都是第一个泉水,它同时也是这棵树的根节点。

题目描述

确定三位朋友可以散步的方式数量。该数字必须对 $10^9+7$ 取模。

输入格式

输入文件的第一行包含两个正整数 $n$ 和 $m$,分别代表公园中泉水的数量和 Peter 想要访问的泉水数量。下一行包含 $n-1$ 个数字 $T_2, T_3, \dots, T_n$,代表对应于这棵树的父节点数组。再下一行包含 $m$ 个不同的数字 $P_1, P_2, \dots, P_m$。

输出格式

在输出文件的第一行,打印所需的方案数。

数据范围

  • $2 \le m \le n \le 400\,000$
  • 保证公园的形状是一棵树(一个无环、连通的无向图)。

子任务

子任务 分值 额外输入限制
1 20 $1 \le m \le n \le 10$
2 60 $1 \le m \le n \le 4\,000$
3 100 $1 \le m \le n \le 400\,000$

样例

样例输入 1

7 3
1 1 2 2 2 3
5 4 7

样例输出 1

3

说明

三位朋友正确的散步方式为: $1\ 2\ 5\ 2\ 4\ 2\ 6\ 2\ 1\ 3\ 7\ 3\ 1$ $1\ 2\ 6\ 2\ 5\ 2\ 4\ 2\ 1\ 3\ 7\ 3\ 1$ $1\ 2\ 5\ 2\ 6\ 2\ 4\ 2\ 1\ 3\ 7\ 3\ 1$

此外,以下是三种无效的散步方式: $1\ 2\ 5\ 2\ 4\ 2\ 1\ 3\ 7\ 3\ 1$(未访问第 $6$ 个泉水) $1\ 2\ 4\ 2\ 5\ 2\ 6\ 2\ 1\ 3\ 7\ 3\ 1$(在访问第 $5$ 个泉水之前访问了第 $4$ 个) $1\ 2\ 4\ 2\ 5\ 2\ 6\ 2\ 4\ 2\ 1\ 3\ 7\ 3\ 1$(散步长度不是最短的)

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.