【置换群】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 ...
随机推荐
- [poj 1947]树dp+背包问题
题目链接:http://poj.org/problem?id=1947 看了很多题解都是直接一遍dfs就搞定的方法,但是我实在是没看懂那个转移方程.最后在茫茫博客中终于发现了一个有逻辑的方法,但是复杂 ...
- URAL1277 Cops and Thieves(最小割)
Cops and Thieves Description: The Galaxy Police (Galaxpol) found out that a notorious gang of thieve ...
- Tile Cut~网络流入门题
Description When Frodo, Sam, Merry, and Pippin are at the Green Dragon Inn drinking ale, they like t ...
- java,jenkins
以前玩的是hudson ,现在玩的是jenkins.以前用的是Tomcat,现在不知道他们怎么不用... 1,装个Jenkins镜像. 2.配置项目: 先取个名字:exchange 配个svn: 构建 ...
- Java之戳中痛点 - (3)三目运算符的两个操作数类型尽量一致
先看一个例子: package com.test; public class TernaryOperator { public static void main(String[] args) { in ...
- TCP(二)
TCP半连接和全连接问题 TCP握手过程详解 如上图所示,关键部分:syns queue(半连接队列)和accept queue(全连接队列) 正常情况下的处理过程如下: 1)当server端收到cl ...
- php中的split函数
字符串分割函数:split函数 <?php $email='microsoft@exam!ple.com'; $domain = split('\.|@|!',$email);//split分割 ...
- 【Foreign】旅行路线 [倍增]
旅行路线 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 3 2 ...
- hdu 2544 最短路 (dijkstra,floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找到两点间最短的距离值. 代码一:(dijkstra算法) #include < ...
- Django【进阶】中间件
中间件 一.概念 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 其 ...