E. Sakurako's Hobby

    传统题 1000ms 256MiB

Sakurako's Hobby

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Problem

For a certain permutation pp∗ Sakurako calls an integer jj reachable from an integer ii if it is possible to make ii equal to jj by assigning i=pii=pi a certain number of times.

If p=[3,5,6,1,2,4],p=[3,5,6,1,2,4], then, for example,44 is reachable from 1,1, because: i=1i=p1=3i=p3=6i=p6=4.i=1 →i=p1=3→i=p3=6→i=p6=4. Now i=4,i=4, so 44 is reachablefrom 1.1.

Each number in the permutation is colored either black or white.

Sakurako defines the function F(i)F(i) as the number of black integers that are reachable from i.i.

Sakurako is interested in F(i)F(i) for each 1in,1≤i≤n, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this.

A∗A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (the number 22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation ( n=3,n=3, but the array contains 44 ).

Input

The first line contains a single integer tt (1t104)(1≤t≤10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n105)(1≤n≤10^5) — the number of elements in the array.

The second line of each test case contains nn integers p1,p2,,pn(1pin)p1,p2,…,pn (1≤pi≤n) — the elements of the permutation.

The third line of each test case contains a string ss of length n,n, consisting of '0' and '1'. If si=0,si=0, then the number pipi is colored black; if si=1,si=1, then the number pipi is colored white.

It is guaranteed that the sum of nn across all test cases does not exceed 51055⋅10^5 .

Output

For each test case, output nn integers F(1),F(2),,F(n).F(1),F(2),…,F(n).

Example : in

5
1
1
0
5
1 2 4 5 3
10101
5
5 4 1 3 2
10011
6
3 5 6 1 2 4
010000
6
1 2 3 4 5 6
100110

Example : out

1 
0 1 1 1 1 
2 2 2 2 2 
4 1 4 4 1 4 
0 1 1 0 0 1 

语法基础(数组、字符串及数据批量存储)+ 压力

未参加
状态
已结束
规则
ACM/ICPC
题目
6
开始于
2024-10-11 19:00
结束于
2024-10-12 0:00
持续时间
5 小时
主持人
参赛人数
7