hdu 4985 Little Pony and Permutation(置换)

题目链接:hdu 4985 Little Pony and Permutation

题目大意:给定一个置换,将置换分解成若干个循环。

解题思路:枚举起点,逐个移动即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 1e5+5;

int N, f[maxn], v[maxn];

int main () {
    while (scanf("%d", &N) == 1) {
        memset(v, 0, sizeof(v));
        for (int i = 1; i <= N; i++)
            scanf("%d", &f[i]);

        for (int i = 1; i <= N; i++) {
            if (v[i])
                continue;

            printf("(%d", i);
            v[i] = 1;
            for (int j = f[i]; v[j] == 0; j = f[j]) {
                printf(" %d", j);
                v[j] = 1;
            }
            printf(")");
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(HDU)