noip模拟赛 轮换

分析:模拟题,关键就是要理解题目意思.m≥3的轮换可以拆成m=2的小轮换,小轮换的话只需要交换一下就可以了.

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

using namespace std;

int n, p, k, f[1010][1010], a[1010], pos[1010];

int main()
{
    scanf("%d%d%d", &n, &p, &k);
    for (int i = 1; i <= n; i++)
        a[i] = i, pos[i] = i;
    for (int i = p; i >= 1; i--)
    {
        scanf("%d", &f[i][0]);
        for (int j = 1; j <= f[i][0]; j++)
            scanf("%d", &f[i][j]);
    }
    for (int i = 1; i <= p; i++)
        for (int j = 2; j <= f[i][0]; j++)
        {
            swap(a[pos[f[i][1]]], a[pos[f[i][j]]]);
            swap(pos[f[i][1]], pos[f[i][j]]);
        }
    for (int i = 1; i <= n; i++)
        printf("%d ", a[i]);

    return 0;
}

 

posted @ 2017-11-02 21:49  zbtrs  阅读(208)  评论(0编辑  收藏  举报