首页 > 代码库 > HDU 4985 Little Pony and Permutation(置换)
HDU 4985 Little Pony and Permutation(置换)
HDU 4985 Little Pony and Permutation
题目链接
题意:给定一个置换,输出分解成的循环
水题,直接模拟即可
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N = 100005; int n, a[N], vis[N]; int main() { while (~scanf("%d", &n)) { memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { if (vis[i]) continue; int t = i; printf("(%d", t); vis[t] = 1; t = a[t]; while (vis[t] == 0) { vis[t] = 1; printf(" %d", t); t = a[t]; } printf(")"); } printf("\n"); } return 0; }
HDU 4985 Little Pony and Permutation(置换)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。