English · Original
Chinese · Translation
Formal · Original
Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: in the one it was directed originally and in the reversed direction.
Pavel has a plan: a permutation _p_ and a sequence _b_1, _b_2, ..., _b__n_, consisting of zeros and ones. Each second Pavel move skewer on position _i_ to position _p__i_, and if _b__i_ equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions.
Unfortunately, not every pair of permutation _p_ and sequence _b_ suits Pavel. What is the minimum total number of elements in the given permutation _p_ and the given sequence _b_ he needs to change so that every skewer will visit each of 2_n_ placements? Note that after changing the permutation should remain a permutation as well.
There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation _p_ and a sequence _b_ suit him if there is an integer _k_ (_k_ ≥ 2_n_), so that after _k_ seconds each skewer visits each of the 2_n_ placements.
It can be shown that some suitable pair of permutation _p_ and sequence _b_ exists for any _n_.
## Input
The first line contain the integer _n_ (1 ≤ _n_ ≤ 2·105) — the number of skewers.
The second line contains a sequence of integers _p_1, _p_2, ..., _p__n_ (1 ≤ _p__i_ ≤ _n_) — the permutation, according to which Pavel wants to move the skewers.
The third line contains a sequence _b_1, _b_2, ..., _b__n_ consisting of zeros and ones, according to which Pavel wants to reverse the skewers.
## Output
Print single integer — the minimum total number of elements in the given permutation _p_ and the given sequence _b_ he needs to change so that every skewer will visit each of 2_n_ placements.
[samples]
## Note
In the first example Pavel can change the permutation to 4, 3, 1, 2.
In the second example Pavel can change any element of _b_ to 1.
Pavel 在烤肉。有 #cf_span[n] 根肉串,它们排成一行,每根位于 #cf_span[n] 个位置中的一个。Pavel 希望每根肉串在两个方向上都经过每一个位置:即最初的方向和反转后的方向。
Pavel 有一个计划:一个排列 #cf_span[p] 和一个由 0 和 1 组成的序列 #cf_span[b1, b2, ..., bn]。每一秒,Pavel 将位于位置 #cf_span[i] 的肉串移动到位置 #cf_span[pi],如果 #cf_span[bi] 等于 #cf_span[1],则将其反转。他希望每根肉串都能访问每一个位置的两个方向。
然而,并非每一对排列 #cf_span[p] 和序列 #cf_span[b] 都符合 Pavel 的要求。他最少需要更改排列 #cf_span[p] 和序列 #cf_span[b] 中的多少个元素,才能使每根肉串访问全部 #cf_span[2n] 个位置?注意,更改后排列仍必须保持为一个排列。
如果某根肉串在烹饪结束前多次访问某些位置,这对 Pavel 来说不是问题。换句话说,如果存在一个整数 #cf_span[k](#cf_span[k ≥ 2n]),使得在 #cf_span[k] 秒后每根肉串都访问过全部 #cf_span[2n] 个位置,则排列 #cf_span[p] 和序列 #cf_span[b] 就是合适的。
可以证明,对于任意 #cf_span[n],都存在一组合适的排列 #cf_span[p] 和序列 #cf_span[b]。
第一行包含整数 #cf_span[n](#cf_span[1 ≤ n ≤ 2·105])——肉串的数量。
第二行包含一个整数序列 #cf_span[p1, p2, ..., pn](#cf_span[1 ≤ pi ≤ n])——Pavel 想用来移动肉串的排列。
第三行包含一个由 0 和 1 组成的序列 #cf_span[b1, b2, ..., bn]——Pavel 想用来决定是否反转肉串的序列。
请输出一个整数——Pavel 至少需要更改排列 #cf_span[p] 和序列 #cf_span[b] 中的多少个元素,才能使每根肉串访问全部 #cf_span[2n] 个位置。
在第一个例子中,Pavel 可以将排列改为 #cf_span[4, 3, 1, 2]。
在第二个例子中,Pavel 可以将 #cf_span[b] 中的任意一个元素改为 #cf_span[1]。
## Input
第一行包含整数 #cf_span[n](#cf_span[1 ≤ n ≤ 2·105])——肉串的数量。第二行包含一个整数序列 #cf_span[p1, p2, ..., pn](#cf_span[1 ≤ pi ≤ n])——Pavel 想用来移动肉串的排列。第三行包含一个由 0 和 1 组成的序列 #cf_span[b1, b2, ..., bn]——Pavel 想用来决定是否反转肉串的序列。
## Output
请输出一个整数——Pavel 至少需要更改排列 #cf_span[p] 和序列 #cf_span[b] 中的多少个元素,才能使每根肉串访问全部 #cf_span[2n] 个位置。
[samples]
## Note
在第一个例子中,Pavel 可以将排列改为 #cf_span[4, 3, 1, 2]。在第二个例子中,Pavel 可以将 #cf_span[b] 中的任意一个元素改为 #cf_span[1]。
**Definitions**
Let $ n \in \mathbb{Z}^+ $ be the number of skewers.
Let $ p \in S_n $ be a permutation of $ \{1, 2, \dots, n\} $.
Let $ b \in \{0,1\}^n $ be a binary sequence.
Each skewer $ i $ at time $ t $ is at position $ p^t(i) $ and has orientation $ \bigoplus_{j=0}^{t-1} b_{p^j(i)} \mod 2 $, where $ p^0(i) = i $ and $ \oplus $ denotes XOR sum.
The state of skewer $ i $ at time $ t $ is the pair $ (p^t(i), \sum_{j=0}^{t-1} b_{p^j(i)} \mod 2) \in \{1,\dots,n\} \times \{0,1\} $, representing position and direction.
There are $ 2n $ distinct placements.
**Constraints**
1. $ 1 \leq n \leq 2 \cdot 10^5 $
2. $ p $ is a permutation of $ \{1, \dots, n\} $
3. $ b_i \in \{0,1\} $ for all $ i \in \{1, \dots, n\} $
**Objective**
Find the minimum number of changes to elements of $ p $ and $ b $ (combined) such that, for every skewer $ i \in \{1, \dots, n\} $, the trajectory $ \left( p^t(i), \sum_{j=0}^{t-1} b_{p^j(i)} \mod 2 \right) $, $ t = 0, 1, 2, \dots $, covers all $ 2n $ placements (i.e., every position-direction pair) within some finite time $ k \geq 2n $.
**Equivalently**, the dynamical system defined by $ (i, d) \mapsto (p(i), d \oplus b_i) $ on the state space $ \{1,\dots,n\} \times \{0,1\} $ must consist of a single cycle of length $ 2n $.
Thus, the problem reduces to:
**Minimize the number of modifications to $ p $ and $ b $ so that the permutation-action graph on $ 2n $ states forms a single cycle of length $ 2n $.**
API Response (JSON)
{
"problem": {
"name": "C. Pavel and barbecue",
"description": {
"content": "Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: i",
"description_type": "Markdown"
},
"platform": "Codeforces",
"limit": {
"time_limit": 2000,
"memory_limit": 262144
},
"difficulty": "None",
"is_remote": true,
"is_sync": true,
"sync_url": null,
"sign": "CF760C"
},
"statements": [
{
"statement_type": "Markdown",
"content": "Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: i...",
"is_translate": false,
"language": "English"
},
{
"statement_type": "Markdown",
"content": "Pavel 在烤肉。有 #cf_span[n] 根肉串,它们排成一行,每根位于 #cf_span[n] 个位置中的一个。Pavel 希望每根肉串在两个方向上都经过每一个位置:即最初的方向和反转后的方向。\n\nPavel 有一个计划:一个排列 #cf_span[p] 和一个由 0 和 1 组成的序列 #cf_span[b1, b2, ..., bn]。每一秒,Pavel 将位于位置 #cf_span[...",
"is_translate": true,
"language": "Chinese"
},
{
"statement_type": "Markdown",
"content": "**Definitions** \nLet $ n \\in \\mathbb{Z}^+ $ be the number of skewers. \nLet $ p \\in S_n $ be a permutation of $ \\{1, 2, \\dots, n\\} $. \nLet $ b \\in \\{0,1\\}^n $ be a binary sequence. \n\nEach skewer $ ...",
"is_translate": false,
"language": "Formal"
}
]
}