#DSU0000025. Sở thích của Sakurako (Sakurako’s Hobby)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sở thích của Sakurako (Sakurako’s Hobby)
Sakurako’s Hobby
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of . Every position has one of two colors specified by a binary string of length : character 0 means black and 1 means white.
From position , you may move to position . From the new position , you may move again to , and may continue as many times as you like. The starting position is also considered reachable. Since is a permutation, the movement will eventually revisit positions.
Let be the number of distinct black positions reachable when starting from . Compute . A position is counted only once, even if it can be visited repeatedly.
Input
The first line contains , the second a permutation , and the third a binary string of length .
Output
Print .
Subtasks
- Subtask 1 (20 points): ; is a permutation of ; and .
- Subtask 2 (30 points): ; is a permutation of ; and .
- Subtask 3 (50 points): ; is a permutation of ; and .
Examples
Input
6
3 5 6 1 2 4
010011
Output
3 0 3 3 0 3
Explanation
The permutation consists of the cycles and . In the first cycle, positions are black, so every position in that cycle can reach exactly three distinct black positions. The second cycle contains no black positions. The result is 3 0 3 3 0 3.