CF756A.Pavel and barbecue

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

帕维尔正在烤肉。一共有 nn 个烤串,它们排成一行,放置在烤架上的 nn 个位置上。帕维尔希望每个烤串都能在全部 nn 个位置上、且在两个方向(即其原始朝向和翻转后的朝向)各被烤制一段时间。

帕维尔有一个计划:一个排列 pp 和一个由 00 和 11 组成的序列 b1,b2,…,bnb_1, b_2, \dots, b_n。每一秒,帕维尔将位于位置 ii 的烤串移动到位置 pip_i;若 bi=1b_i = 1,则同时将其翻转。因此,他期望每个烤串最终都能访问全部 2n2n 种“摆放状态”(即 nn 个位置 ×\times 两种方向)。

遗憾的是,并非任意给定的排列 pp 和序列 bb 都能满足帕维尔的要求。那么,为使每个烤串最终都能访问全部 2n2n 种摆放状态,帕维尔至少需要修改排列 pp 和序列 bb 中多少个元素?注意:修改后,pp 仍须是一个合法排列。

只要某个烤串在完成全部烤制前多次访问某些摆放状态,对帕维尔而言并无问题。换言之,排列 pp 和序列 bb 满足要求,当且仅当存在某个整数 kk(k≥2nk \geq 2n),使得经过 kk 秒后,每个烤串都曾访问过全部 2n2n 种摆放状态。

可以证明:对任意正整数 nn,总存在满足要求的排列 pp 和序列 bb。

输入格式

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.

第一行包含整数 nn(1 ≤ n ≤ 2⋅1051 ≤ n ≤ 2·10^5)—— 烤肉签的数量。

第二行包含一个整数序列 p1, p2, ..., pnp_1, p_2, ..., p_n(1 ≤ pi ≤ n1 ≤ p_i ≤ n)—— Pavel 希望按照该排列移动烤肉签。

第三行包含一个由 0 和 1 组成的序列 b1, b2, ..., bnb_1, b_2, ..., b_n,Pavel 将依据该序列决定是否翻转对应的烤肉签。

输出格式

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.

输出一个整数——为使每个烤肉签访问全部 2n2n 个位置,所需修改给定排列 pp 和给定序列 bb 中元素的最少总个数。

输入输出样例

  • 输入#1

    4
    4 3 2 1
    0 1 1 1

    输出#1

    2
  • 输入#2

    3
    2 3 1
    0 0 0

    输出#2

    1

说明/提示

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.

在第一个例子中,帕维尔可以将排列更改为 4, 3, 1, 24,\ 3,\ 1,\ 2。

在第二个例子中,帕维尔可以将 bb 的任意一个元素更改为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页