【置换群】Codeforces Round #393 (Div. 1) A. Pavel and barbecue
就是先看排列p,必须满足其是一个环,才满足题意。就处理出有几个环,然后把它们合起来,答案就是多少。
然后再看序列b,自己稍微画一画就会发现,如果有偶数个1肯定是不行哒,否则,它就会再置换一圈回到它自己的位置的时候,正反面的情况和最初始相同,这样怎么转都没法在每个位置烤两面。
所以两部分的答案加起来就是最后的答案。
2 seconds
256 megabytes
standard input
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.
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.
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.
- 4
4 3 2 1
0 1 1 1
- 2
- 3
2 3 1
0 0 0
- 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.
- #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的更多相关文章
- 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 ...
- Codeforces Round #393 (Div. 2)
A. Petr and a calendar time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- 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个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...
- Codeforces Round #393 (Div. 2) - C
题目链接:http://codeforces.com/contest/760/problem/C 题意:有n个烤串,并且每个烤串起初都放在一个火盆上并且烤串都正面朝上,现在定义p序列,p[i]表示在i ...
- Codeforces Round #393 (Div. 2) - B
题目链接:http://codeforces.com/contest/760/problem/B 题意:给定n张床,m个枕头,然后给定某个特定的人(n个人中的其中一个)他睡第k张床,问这个人最多可以拿 ...
- Codeforces Round #393 (Div. 2) - A
题目链接:http://codeforces.com/contest/760/problem/A 题意:给定一个2017年的月份和该月的第一天的星期,问该月份的日历表中需要多少列.行有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 ...
- 【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack
就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁. 用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1.相当于寻找 ...
- 【二分】【动态规划】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 ...
随机推荐
- [hdu 4417]树状数组+离散化+离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和.主要问题是这个数组是静态的,如果带修改 ...
- mysql__视图
视图 1.什么是视图 视图是一种虚拟存在的表,对于使用视图的用户来说基本上是透明的.视图并不是在数据库中实际存在的,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的 视图相对于普通 ...
- 新手如何更换自己喜欢的背景以及此背景的css码
以下内容为转载(对于css码可以自己写当然也可以去网上搜现成的): 更换背景教学:https://jingyan.baidu.com/album/fc07f9897c730412ffe519c0.ht ...
- 51nod 拉勾专业算法能力测评消灭兔子 优先队列+贪心
题目传送门 这道题一开始想了很久...还想着写网络流 发现根本不可能.... 然后就想着线段树维护然后二分什么的 最后发现优先队列就可以了 代码还是很简洁的啦 233 就是把兔子按血量从大到小排序一下 ...
- 【BZOJ】ARC083 E - Bichrome Tree
[算法]树型DP [题意]给定含n个点的树的形态,和n个数字Xv,要求给每个点赋予黑色或白色和权值,满足对于每个点v,子树v中和v同色的点的权值和等于Xv.n<=10^5 [题解]首先每个点的权 ...
- swift中_的用法,忽略默认参数名。
swift中默认参数名除了第一个之外,其他的默认是不忽略的,但是如果在参数的名字前面加上_,就可以忽略这个参数名了,虽然有些麻烦,但是这种定义也挺好,而且不想知道名字或者不想让别人知道名字的或者不用让 ...
- bzoj 2142 国家集训队试题 礼物
问题转化成求C(N,M) mod P p为非素数,那么我们可以将P分解质因数, 也就是 π pi^ci的形式,因为这些pi^ci是互质的,所以我们可以用crt将他们合并 那么问题就转化成了快速求C(N ...
- cve-2012-5613 mysql本地提权
cve-2012-5613 是一个通过FILE权限写Trigger的TRG存储文件(即伪造Trigger),由root触发而导致权限提升的漏洞.不知道为什么这个漏洞一直没修,可能mysql认为这是一 ...
- 千万不要使用xfce和KDE版Manjaro Linux--之荒谬言论
Manjaro Linux 使用经验: ①千万不要使用xfce版,虽然性能上廉价,但是吃亏,调声音80%几率卡死调不了,托盘图标很容易不响应!关机的Beep声,分分钟吓死人!按照网上各种方法弄,下次开 ...
- UVALIVE 2686 Stargates
尼玛真深坑合时p[x] = y 就RE,p[y] = x 就AC . #include <map> #include <set> #include <list> # ...