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

分析:先考虑环的个数,环个数>1,答案加上环个数;

   在环内,2个面都能访问到每个位置当仅当翻面次数为奇数次;

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <bitset>
  11. #include <map>
  12. #include <queue>
  13. #include <stack>
  14. #include <vector>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define mod 1000000007
  17. #define inf 0x3f3f3f3f
  18. #define vi vector<int>
  19. #define pb push_back
  20. #define mp make_pair
  21. #define fi first
  22. #define se second
  23. #define ll long long
  24. #define pi acos(-1.0)
  25. #define pii pair<int,int>
  26. #define sys system("pause")
  27. const int maxn=2e5+;
  28. using namespace std;
  29. inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  30. inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  31. inline void umax(ll &p,ll q){if(p<q)p=q;}
  32. inline void umin(ll &p,ll q){if(p>q)p=q;}
  33. inline ll read()
  34. {
  35. ll x=;int f=;char ch=getchar();
  36. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  37. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  38. return x*f;
  39. }
  40. int n,m,k,t,p[maxn],ret,vis[maxn];
  41. int main()
  42. {
  43. int i,j;
  44. scanf("%d",&n);
  45. rep(i,,n)p[i]=read();
  46. rep(i,,n)
  47. {
  48. k=read();
  49. if(k==)++j;
  50. }
  51. ret+=j%==;
  52. rep(i,,n)
  53. {
  54. if(!vis[i])t++;
  55. int pos=i;
  56. while(!vis[pos])
  57. {
  58. vis[pos]=,pos=p[pos];
  59. }
  60. }
  61. ret+=t!=?t:;
  62. printf("%d\n",ret);
  63. return ;
  64. }

Pavel and barbecue的更多相关文章

  1. 【置换群】Codeforces Round #393 (Div. 1) A. Pavel and barbecue

    就是先看排列p,必须满足其是一个环,才满足题意.就处理出有几个环,然后把它们合起来,答案就是多少. 然后再看序列b,自己稍微画一画就会发现,如果有偶数个1肯定是不行哒,否则,它就会再置换一圈回到它自己 ...

  2. 【codeforces 760C】Pavel and barbecue

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. Pavel and barbecue CodeForces - 756A (排列,水题)

    大意: 给定排列p, 0/1序列b, 有n个烤串, 每秒钟第i串会移动到$p_i$, 若$p_i$为1则翻面, 可以修改b和p, 求最少修改次数使得每串在每个位置正反都被烤过. 显然只需要将置换群合并 ...

  4. CF760 C. Pavel and barbecue 简单DFS

    LINK 题意:给出n个数,\(a_i\)代表下一步会移动到第\(a_i\)个位置,并继续进行操作,\(b_i\)1代表进行一次翻面操作,要求不管以哪个位置上开始,最后都能满足 1.到达过所有位置 2 ...

  5. Codeforces 760C:Pavel and barbecue(DFS+思维)

    http://codeforces.com/problemset/problem/760/C 题意:有n个盘子,每个盘子有一块肉,当肉路过这个盘子的时候,当前朝下的这一面会被煎熟,每个盘子有两个数,p ...

  6. CodeForces 760 C. Pavel and barbecue(dfs+思维)

    题目链接:http://codeforces.com/contest/760/problem/C 题意:一共有N个烤炉,有N个烤串,一开始正面朝上放在N个位子上.一秒之后,在位子i的串串会移动到pi位 ...

  7. 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 ...

  8. Codeforces Round #393 (Div. 2)

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

  9. Codeforces 1119E Pavel and Triangles (贪心)

    Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal t ...

随机推荐

  1. android在学习——activity关闭和dialog.dismiss冲突的解决(Activity has leaked window com.android.internal.policy.impl.PhoneWindow)

    当我们在退出整个程序的时候偶尔会出现这种报错:Activity has leaked window com.android.internal.policy.impl.PhoneWindow 其意思大概 ...

  2. 【BZOJ 1572】 工作安排

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1572 [算法] 贪心 先将这些工作按截至时间排序 建立一个小根堆,当决策是否完成一项 ...

  3. Shuffle'm Up(串)

    http://poj.org/problem?id=3087 题意:每组3个串,前两个串长度为n,第三个串长度为2*n,依次从第二个串(s2)中取一个字符,从第一个串(s1)中取一个字符,...... ...

  4. WebService的概念

    一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...

  5. Oracle 批量插入值

    工作中常遇到将Excel文档数据转为SQL语句,然后再将SQL语句插入到数据库已完成数据转移保存到数据库中,下面介绍下如何一次性插入多条SQL语句,先抛个图: 由于真实数据不变给大家看,所以这里是做了 ...

  6. MYSQL 数据库命令行终端操作笔记

    1.数据库登录: 1.登录本地的MYSQL数据库:mysql -u root -p   2.连接远程主机上的MYSQL数据库:mysql -h 192.168.191.2 -u root -p 123 ...

  7. mvc3结合spring.net-依赖注入

    namespace Tuzi.Models.IService { public interface IPersonService { string say(string words); } names ...

  8. JavaScript Array 整理 - 元素操作

    整理一下Array对象中针对元素操作的方法. 分别是: concat (组合数组) join(数组转字符串) pop(删除最后一个元素) shift(删除第一个元素) push(在数组尾部添加新元素) ...

  9. 魅族和三星Galaxy 5.0webView 问题Android Crash Report - Native crash at /system/lib/libc.so caused by webvi

    解决办法是当前activity 销毁的时候 webView.destroy(); hine: ConnectedState (when=-2ms what= arg1=!CMD_RSSI_POLL : ...

  10. Flutter GitLab 客户端

    F4Lab Flutter for GitLab. 欢迎参加一起完成