博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【置换群】Codeforces Round #393 (Div. 1) A. Pavel and barbecue
阅读量:5734 次
发布时间:2019-06-18

本文共 2831 字,大约阅读时间需要 9 分钟。

就是先看排列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
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;}

转载于:https://www.cnblogs.com/autsky-jadek/p/6343284.html

你可能感兴趣的文章
Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)
查看>>
Mina2.0框架源码剖析(七)
查看>>
MIME类型
查看>>
第 3 章 Berkeley UNIX C shell (csh)
查看>>
LIST<T>现在也支持序列化和反序列化了
查看>>
【转】Android世界的Swift - Kotlin语言
查看>>
基于Spring Boot的Logback日志轮转配置
查看>>
3.2. Access Privilege System
查看>>
基于Metronic的Bootstrap开发框架经验总结(18)-- 在代码生成工具Database2Sharp中集成对Bootstrap-table插件的分页及排序支持...
查看>>
用原型继承方法
查看>>
JPG、PNG和GIF图片的基本原理及优…
查看>>
linux内存条排查
查看>>
解决cacti监控windows网卡带有中文
查看>>
梁念坚:“云计算”福音
查看>>
管理软件的飞跃:像用自来水一样用
查看>>
四块固态硬盘联合刷新PCMark05世界记录
查看>>
浅析信息化时代 医院混合云建设模式
查看>>
Gigamon针对AWS引入全面可视化平台
查看>>
DTCC2015议程曝光 最新嘉宾议题揭秘
查看>>
BAT、IBM、亚马逊、微软等一线互联网的区块链版图布局
查看>>