Pavel and barbecue
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.
分析:先考虑环的个数,环个数>1,答案加上环个数;
在环内,2个面都能访问到每个位置当仅当翻面次数为奇数次;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=2e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,p[maxn],ret,vis[maxn];
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)p[i]=read();
rep(i,,n)
{
k=read();
if(k==)++j;
}
ret+=j%==;
rep(i,,n)
{
if(!vis[i])t++;
int pos=i;
while(!vis[pos])
{
vis[pos]=,pos=p[pos];
}
}
ret+=t!=?t:;
printf("%d\n",ret);
return ;
}
Pavel and barbecue的更多相关文章
- 【置换群】Codeforces Round #393 (Div. 1) A. Pavel and barbecue
就是先看排列p,必须满足其是一个环,才满足题意.就处理出有几个环,然后把它们合起来,答案就是多少. 然后再看序列b,自己稍微画一画就会发现,如果有偶数个1肯定是不行哒,否则,它就会再置换一圈回到它自己 ...
- 【codeforces 760C】Pavel and barbecue
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Pavel and barbecue CodeForces - 756A (排列,水题)
大意: 给定排列p, 0/1序列b, 有n个烤串, 每秒钟第i串会移动到$p_i$, 若$p_i$为1则翻面, 可以修改b和p, 求最少修改次数使得每串在每个位置正反都被烤过. 显然只需要将置换群合并 ...
- CF760 C. Pavel and barbecue 简单DFS
LINK 题意:给出n个数,\(a_i\)代表下一步会移动到第\(a_i\)个位置,并继续进行操作,\(b_i\)1代表进行一次翻面操作,要求不管以哪个位置上开始,最后都能满足 1.到达过所有位置 2 ...
- Codeforces 760C:Pavel and barbecue(DFS+思维)
http://codeforces.com/problemset/problem/760/C 题意:有n个盘子,每个盘子有一块肉,当肉路过这个盘子的时候,当前朝下的这一面会被煎熟,每个盘子有两个数,p ...
- CodeForces 760 C. Pavel and barbecue(dfs+思维)
题目链接:http://codeforces.com/contest/760/problem/C 题意:一共有N个烤炉,有N个烤串,一开始正面朝上放在N个位子上.一秒之后,在位子i的串串会移动到pi位 ...
- 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 1119E Pavel and Triangles (贪心)
Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal t ...
随机推荐
- jabberNet 发送出席信息
没代码我说个J8: public void Presence(User.EStatus status) { string statustxt = ""; //说明文字.比如,离开的 ...
- pat1013:数素数
https://www.patest.cn/contests/pat-b-practise/1013 #include "stdio.h" #include "math. ...
- u-boot的内存分布和全局数据结构
U-boot,除非在RAM中调试,一般情况下都是从flash中执行一段代码,然后将flash中储存的代码和数据搬移到ram中,然后跳转到ram中执行.当然这应该也是一般的bootloader的执行方式 ...
- .NET页面事件执行顺序
摘自:http://www.cnblogs.com/kenkofox/archive/2011/03/18/1987998.html和http://blog.csdn.net/yiruoyun/art ...
- Balloons(DFS)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2248 题意:(1)求图中四连块(有公共边的方块 ...
- jquery的ajax同步异步执行
大家先看一段简单的jquery ajax 返回值的js 代码 function getReturnAjax{ $.ajax({ type:"POST", http:/ ...
- codevs1253 超级市场(dp)
1253 超级市场 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 某人喜欢按照自己的规则去市场买菜,他每天 ...
- Python 38 注册和修改密码
一:注册系统服务 1.添加环境变量:桌面点击我的电脑------>右键属性------>双击高级系统设置------>点击环境变量------>找到在系统变量中的Path后双击 ...
- LocalDateTime查找最近的五分钟点
/** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateT ...
- python之路——常用模块
阅读目录 认识模块 什么是模块 模块的导入和使用 常用模块一 collections模块 时间模块 random模块 os模块 sys模块 序列化模块 re模块 常用模块二 hashlib模块 con ...