#C. 清理数字

    Type: Default File IO: clear 1000ms 256MiB

清理数字

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

清理数字(clear)

法师宁宁作为一个魔女,拥有一颗樱花树的掌控权。

这颗樱花树大小为 nn,用 n1n-1 条边连接,以 11 为根。每个点上有数字 0\tt01\tt1

你需要按一定顺序清理这些节点上的数字,一个节点是可清理的当且仅当它为根或其父亲被清理。

按清理顺序,节点上的数字形成一个 01\tt 01 串,你要最小化当中逆序对数。

输入格式

第一行一个正整数表示 nn

接下来 n1n-1 个数,表示点 i+1i+1 的父亲。

接下来 nn 个正整数 aia_i 表示节点上的数字。

输出格式

一行一个非负整数表示答案。

测试样例

样例输入 样例输出
61 1 2 3 30 1 1 0 0 0 4
10 0
151 2 3 2 5 6 2 2 9 10 1 12 13 121 1 1 0 1 1 0 0 1 0 0 1 1 0 0 31

样例解释

样例 11 依次清理 1,3,5,6,2,41,3,5,6,2,4 即可。

数据范围

测试点编号 nn\leq fif_i 的性质 特殊性质
121\sim 2 1010
33 10510^5 fi=i1f_i=i-1
474\sim 7 10310^3 fif_i[1,n1][1,n-1] 当中随机生成 aia_i 随机生成
8108\sim 10 10510^5
  • 时间限制:1s\tt1s
  • 空间限制:128MB\tt 128MB

NOIP模拟赛(八)

Not Attended
Status
Done
Rule
IOI
Problem
4
Start at
2023-11-16 8:00
End at
2023-11-16 12:00
Duration
4 hour(s)
Host
Partic.
20