Codeforces 612E 置换群
来源:互联网 发布:远程网络监控 编辑:程序博客网 时间:2024/05/20 18:40
Codeforces 612E
题目链接:
http://codeforces.com/problemset/problem/612/E
题意:
存在一个1-n的排列p。
现在定义一个1-n的排列q,使得q[i] = p[p[i]]。
给出q,输出一个合法的p。不存在输出-1。
思路:
显然置换群。置换群能分成若干个不相交的环。
环,首先有个可以自己手动模拟出的结论。
1.环的元素个数是偶数,经过一次映射后,环会分裂成两个元素大小相等的环。
2.环的元素个数是奇数,经过一次映射后,环只是移动位置而不会发生分裂。
那么具体如何分裂?假设当前从1出发,经过两次映射后到3、再到5…
故容易知道环的元素个数为奇数则不会发生分裂,且相对于原来的顺序是1357…2468..
故容易知道环的元素个数为偶数会发生分裂,且相对于原来的顺序1357…、2468…
那么,现在求逆运算的时候,如果遇到奇数环,是可逆的;偶数环,和另外一个相同长度的偶数环合并。
源码:
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <algorithm>#include <iostream>#include <vector>using namespace std;const int MAXN = 1e6 + 5;int pre[MAXN];int vis[MAXN];int p[MAXN], q[MAXN];vector<int>vc1, vc2;void combine_even(int x, int y){ vc1.clear(); int org = x; do{// printf("x = %d, y = %d\n", x, y); vc1.push_back(x); x = p[x]; vc1.push_back(y); y = p[y]; }while(x != org);// printf("vc1.size() = %d\n", vc1.size()); for(int i = 0 ; i < (int)vc1.size() ; i++){ q[vc1[i]] = vc1[(i + 1) % (int)vc1.size()];// printf("for x = %d, y = %d, q[vc1[i]] = %d, vc1[(i+1)%(int(vc1.size()) = %d\n", x, y, q[vc1[i]], vc1[(i+1)%(int)vc1.size()]); }}void combine_odd(int x, int cnt){// printf("x = %d\n",x ); vc1.clear(); int num = 0; int y = x; while(num < cnt / 2 + 1){ y = p[y]; num++; }// printf("x = %d, y = %d\n", x, y); num = 0; while(num < cnt / 2){ vc1.push_back(x); x = p[x]; vc1.push_back(y); y = p[y]; num++;// printf("x = %d, y = %d\n", x, y); } vc1.push_back(x); for(int i = 0 ; i < (int)vc1.size() ; i++){ q[vc1[i]] = vc1[(i + 1) % (int)vc1.size()]; }}int main(){ int n; while(scanf("%d", &n) != EOF){ memset(vis, 0, sizeof(vis)); memset(pre, 0, sizeof(pre)); memset(q, 0, sizeof(q)); for(int i = 1 ; i <= n ; i++) scanf("%d", &p[i]); for(int i = 1 ; i <= n ; i++) if(vis[i] == 0){ int now = i; int cnt = 0; while(vis[now] == 0) vis[now] = 1, now = p[now], cnt++;// printf("i = %d, cnt = %d\n", i, cnt); if(cnt % 2 == 0){ if(pre[cnt] == 0) pre[cnt] = i; else{// printf("first\n"); combine_even(pre[cnt], i); pre[cnt] = 0; } } else combine_odd(i, cnt); } int ok = 1; for(int i = 1 ; i <= n ; i++){ if(pre[i] != 0 || q[i] == 0){// printf("i = %d, pre[i] = %d, q[i] = %d\n", i, pre[i], q[i]); ok = 0; break; } } if(ok == 0) printf("-1\n"); else{ for(int i = 1 ; i <= n ; i++){ printf("%d", q[i]); if(i == n) printf("\n"); else printf(" "); } } } return 0;}
0 0
- Codeforces 612E 置换群
- Codeforces 612E Square Root of Permutation 【置换群】
- CodeForces 612e Square Root of Permutation(置换+构造)
- 【CF#612E】Square Root of Permutation 置换群
- Codeforces 612E
- Codeforces Round #324 (Div. 2) (584A,584B(排列组合),584C(构造),584D(哥德巴赫猜想),584E(置换群))
- Educational Codeforces Round 4 E. Square Root of Permutation(置换、构造)
- 置换 置换群 应用
- 置换 置换群 应用
- 置换 置换群 应用
- Codeforces Round #252 D 置换群的简单应用
- 置换群
- 置换群
- 置换群
- 置换群
- Codeforces 612 E Square Root of Permutation
- Codeforces 612E - Square Root of Permutation
- codeforces 163E e-Government
- java中UDP服务器和客户端
- POJ 2376 Cleaning Shifts(贪心,区间问题)
- word或wps中mathtype在工具栏/菜单栏消失的解决方法
- Arcgis版本问题
- 均方根信息滤波(SRIF)测试(一)
- Codeforces 612E 置换群
- 微信 chooseWXPay fail问题解决(非支付目录配置问题),支付接口参数问题
- 服务器
- DIV CSS遮罩层
- hdfs目录创建hive表
- NJUPT GNU/Linux编程 实验1-6总结(走过的坑)
- 客户端(client)
- redis安装
- 点击软键盘搜索键搜索事件(启动Activity)被执行两次