就是先看排列p,必须满足其是一个环,才满足题意。就处理出有几个环,然后把它们合起来,答案就是多少。

然后再看序列b,自己稍微画一画就会发现,如果有偶数个1肯定是不行哒,否则,它就会再置换一圈回到它自己的位置的时候,正反面的情况和最初始相同,这样怎么转都没法在每个位置烤两面。

所以两部分的答案加起来就是最后的答案。

A. Pavel and barbecue
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi 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 2n 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 ≥ 2n), so that after k seconds each skewer visits each of the 2nplacements.

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 p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers.

The third line contains a sequence b1, b2, ..., bn 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 2n placements.

Examples
input
4
4 3 2 1
0 1 1 1
output
2
input
3
2 3 1
0 0 0
output
1
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.

#include<cstdio>
using namespace std;
int p[200100],n,ans;
bool b[200100],vis[200100];
int main()
{
//freopen("a.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&p[i]);
for(int i=1;i<=n;++i)
{
if(vis[i])
continue;
++ans;
vis[i]=1;
int U=p[i];
while(U!=i)
{
vis[U]=1;
U=p[U];
}
}
int cnt=0;
for(int i=1;i<=n;++i)
{
scanf("%d",&b[i]);
cnt+=b[i];
}
printf("%d\n",(ans==1 ? 0 : ans)+(cnt%2==0));
return 0;
}

【置换群】Codeforces Round #393 (Div. 1) A. Pavel and barbecue的更多相关文章

  1. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Codeforces Round #393 (Div. 2)

    A. Petr and a calendar time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  3. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  4. Codeforces Round #393 (Div. 2) - C

    题目链接:http://codeforces.com/contest/760/problem/C 题意:有n个烤串,并且每个烤串起初都放在一个火盆上并且烤串都正面朝上,现在定义p序列,p[i]表示在i ...

  5. Codeforces Round #393 (Div. 2) - B

    题目链接:http://codeforces.com/contest/760/problem/B 题意:给定n张床,m个枕头,然后给定某个特定的人(n个人中的其中一个)他睡第k张床,问这个人最多可以拿 ...

  6. Codeforces Round #393 (Div. 2) - A

    题目链接:http://codeforces.com/contest/760/problem/A 题意:给定一个2017年的月份和该月的第一天的星期,问该月份的日历表中需要多少列.行有7列表示星期一~ ...

  7. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card

    D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...

  8. 【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack

    就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁. 用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1.相当于寻找 ...

  9. 【二分】【动态规划】Codeforces Round #393 (Div. 1) B. Travel Card

    水dp,加个二分就行,自己看代码. B. Travel Card time limit per test 2 seconds memory limit per test 256 megabytes i ...

随机推荐

  1. CVPR2014 Objectness 源码转换(完整版) VS2012 X64 –>win32

    一.版本转换  1.将源码中vs2012 X64版本转换为vs2012 win32版本. 2.源码下载及其相关资料下载http://mmcheng.net/zh/bing/ 3.需要下载源码(Pape ...

  2. [SDOI2011]消防/[NOIP2007] 树网的核

    消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的 ...

  3. Codeforces Round #524 (Div. 2) B. Margarite and the best present

    B. Margarite and the best present 题目链接:https://codeforces.com/contest/1080/problem/B 题意: 给出一个数列:an=( ...

  4. sperman系数

    https://baike.baidu.com/item/spearman%E7%9B%B8%E5%85%B3%E7%B3%BB%E6%95%B0/7977847?fr=aladdin https:/ ...

  5. CentOS 7 主机加固手册-下

      CentOS 7 主机加固手册-上 CentOS 7 主机加固手册-中 CentOS 7 主机加固手册-下 0x1f 删除禁用非必要的服务 删除非必要的服务 # Remove yum remove ...

  6. [Leetcode Week9]Word Break

    Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...

  7. objc_msgSend arm64 崩溃问题

    http://blog.csdn.net/chenyong05314/article/details/42121001 2014-12-24 10:49 878人阅读 评论(0) 收藏 举报 转载自: ...

  8. JQ面向对象

    静态方法:某种类型才有的方法,这个方法干的事情只有类型本身有关,不受具体实例对象的影响,在C#语言中,它用static表示,VB中用share表示,而在jq中我们一般用$或者JQuery表示JQ类型, ...

  9. BZOJ 3038 上帝造题的七分钟二

    无题目 但是百度会发现题目和3211基本一致 所以看上一篇博文的上一篇博文呢

  10. OC学习篇之---代理模式

    何实现代理模式的. 这里举一个简单的例子: 小孩类,护士类,保姆类,其中小孩类有两个方法:wash和play 这里代理对象就是:护士类.保姆类,小孩类是被代理对象. 看一下代码: 首先看一下小孩类: ...