Codeforces Round #393 (Div. 2)-C. Pavel and barbecue(并查集)
2017-01-23 20:45
441 查看
记录一个菜逼的成长。。
题目链接
PS:题目意思实在是难看懂。。
题目大意:
有n个串,n个位置,初始一 一对应。
给你一个排列,p1,p2,...,pn,表示在第i个位置的串需要移动到第pi
给你一个01序列,b1,b2,...,bn表示如果bi为1,那么移动到第i个位置的串需要翻转。
问要改变排列或序列的个数为多少,使得对于每个串在每个位置都有正反两个状态。
会发现,只要当排列是一个环时,一个串才会访问所有的位置。
比如一个排列4 3 1 2表示
1->4
2->3
3->1
4->2
经过几轮移动
1 2 3 4
3 4 2 1
2 1 4 3
4 3 2 1
1 2 3 4
而序列b
只有当1的个数是奇数时,一个串经过一轮其他位置的翻转,回到原来的位置时状态才发生改变。
比如0 1 1 1
假设初始都是正面
经过以上移动,第一个串回到原来位置时是反面。
所以这样反几轮后,每个串都会在每个位置有两个状态。
如果只有一个环,p排列的改变次数是0。如果大于1的话就是环的个数。
而b序列是偶数为1,奇数为0;
题目链接
PS:题目意思实在是难看懂。。
题目大意:
有n个串,n个位置,初始一 一对应。
给你一个排列,p1,p2,...,pn,表示在第i个位置的串需要移动到第pi
给你一个01序列,b1,b2,...,bn表示如果bi为1,那么移动到第i个位置的串需要翻转。
问要改变排列或序列的个数为多少,使得对于每个串在每个位置都有正反两个状态。
会发现,只要当排列是一个环时,一个串才会访问所有的位置。
比如一个排列4 3 1 2表示
1->4
2->3
3->1
4->2
经过几轮移动
1 2 3 4
3 4 2 1
2 1 4 3
4 3 2 1
1 2 3 4
而序列b
只有当1的个数是奇数时,一个串经过一轮其他位置的翻转,回到原来的位置时状态才发生改变。
比如0 1 1 1
假设初始都是正面
经过以上移动,第一个串回到原来位置时是反面。
所以这样反几轮后,每个串都会在每个位置有两个状态。
如果只有一个环,p排列的改变次数是0。如果大于1的话就是环的个数。
而b序列是偶数为1,奇数为0;
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 200000 + 10; int pre[maxn]; int findr(int x){return pre[x] == x?pre[x]:pre[x] = findr(pre[x]);} int unio(int x,int y) { int fx = findr(x),fy = findr(y); if(fx != fy) pre[fx] = fy; } int main() { int n; while(~scanf("%d",&n)){ for( int i = 1; i <= n; i++ )pre[i] = i; int ans = 0; for( int i = 1; i <= n; i++ ){ int x; scanf("%d",&x); unio(x,i); } for( int i = 1; i <= n; i++ ) if(pre[i] == i)ans++; if(ans == 1)ans = 0; int cnt = 0; for( int i = 1; i <= n; i++ ){ int x; scanf("%d",&x); cnt += x; } if(!(cnt&1))ans++; printf("%d\n",ans); } return 0; }
相关文章推荐
- Codeforces Round #393 (Div. 2) - C. Pavel and barbecue
- 【置换群】Codeforces Round #393 (Div. 1) A. Pavel and barbecue
- Codeforces Round #393 (Div. 2) C Pavel and barbecue(思维)
- codeforces round305 div2 Mike and Feet 排序+并查集
- Codeforces 760C Pavel and barbecue【思维+Dfs】
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary(带权并查集)
- CodeForces 760C Pavel and barbecue【英文题】
- Codeforces Round #383 (Div. 2)D.Arpa's weak amphitheater and Mehrdad's valuable Hoses【并查集+分组背包】
- [欧拉回路 并查集] Codeforces 547D #305 (Div. 1) D. Mike and Fish
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #393 Pavel and barbecue
- C. Pavel and barbecue
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(关系并查集)
- Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
- 【codeforces 760C】Pavel and barbecue
- Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
- 【HDU5652 BestCoder Round 77 (div1) B】【并查集 or 二分答案BFS】India and China Origins 两国中间出现山脉 最早时间断绝联系