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 \in \{1, \dots, n\} $ is associated with a state $ (i, d) \in \{1, \dots, n\} \times \{0,1\} $, representing its position and direction ($ d = 0 $: original, $ d = 1 $: reversed). There are $ 2n $ total placements.
The dynamics are:
At each second, skewer at position $ i $ with direction $ d $ moves to position $ p[i] $ and direction $ d \oplus b[i] $.
**Constraints**
1. $ 1 \leq n \leq 2 \cdot 10^5 $
2. $ p $ is a permutation: $ p_i \in \{1, \dots, n\} $, all distinct.
3. $ b_i \in \{0,1\} $ for all $ i $.
**Objective**
Find the minimum total number of changes to elements of $ p $ and $ b $ such that, under the updated $ p $ and $ b $, the state transition graph on the $ 2n $ placements consists of a single cycle of length $ 2n $ (i.e., the system is a single cyclic orbit covering all placements).
Equivalently:
Minimize $ |\{ i \mid p_i \neq p_i' \}| + |\{ i \mid b_i \neq b_i' \}| $,
over all $ p' \in S_n $, $ b' \in \{0,1\}^n $, such that the permutation $ \sigma: (i,d) \mapsto (p'[i], d \oplus b'[i]) $ is a single $ 2n $-cycle.
API Response (JSON)
{
"problem": {
"name": "A. 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": "CF759A"
},
"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[i] 上的...",
"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"
}
]
}