标签:rtu ota wan space and eve 修改 def 理解
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 b 1,?b 2,?...,?b n, consisting of zeros and ones. Each second Pavel move skewer on position i to position p i, and if b i 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 2n placements.
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·10^5)\) — the number of skewers.
The second line contains a sequence of integers \(p_1,?p_2,?...,?p_n (1?≤?p_i?≤?n)\) — the permutation, according to which Pavel wants to move the skewers.
The third line contains a sequence \(b_1,?b_2,?...,?b_n\) 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.
有n个位置和n个肉串,需要把每个串串在每个地方两面都烤过才可以吃,现在有一个序列p,\(p_i\)表示在放在i的肉串下一个都被放到\(p_i\)。有序列b,当\(b_i\)为1时,这个位置要进行翻面。让你对p和b的元素进行最少的修改,让所有肉串都能吃。
理解一下第一个样例的NOTE的4,3,1,2:
让每个肉串在每个地方烤过
让每个肉串在每个地方两面都烤过
#include<bits/stdc++.h>
using namespace std;
const int MAXN=200005;
int n;
int p[MAXN],b[MAXN];
bool vis[MAXN];
void dfs(int x){
while(!vis[x]){
vis[x]=true;
x=p[x];
}
}
int main(){
scanf("%d",&n);
int ans=0;
for(int i=1;i<=n;i++) scanf("%d",&p[i]);
int cnt=0;
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
if(b[i]==1) cnt++;
}
if(cnt%2==0) ans++;
cnt=0;
for(int i=1;i<=n;i++){
if(vis[i]) continue;
dfs(i);
cnt++;
}
if(cnt!=1) ans+=cnt;
printf("%d\n",ans);
return 0;
}
Pavel and barbecue - CF756A 【DFS 思维】
标签:rtu ota wan space and eve 修改 def 理解
原文地址:https://www.cnblogs.com/xuwanwei/p/12833841.html