码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4985 Little Pony and Permutation(置换)

时间:2014-09-01 15:39:03      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   ar   for   代码   sp   

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(置换)

标签:style   http   color   os   io   ar   for   代码   sp   

原文地址:http://blog.csdn.net/accelerator_/article/details/38980213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!