您的位置:首页 > 其它

【HDU】4985 Little Pony and Permutation 置换群

2014-09-04 15:40 405 查看
传送门:【HDU】4985 Little Pony and Permutation

题目分析:基本置换群。内部排序反而错!

代码如下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )

const int MAXN = 100005 ;

int a[MAXN] ;
bool vis[MAXN] ;
int S[MAXN] , top ;
int n ;

void scanf ( int& x , char c = 0 ) {
	while ( ( c = getchar () ) < '0' || c > '9' ) ;
	x = c - '0' ;
	while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}

void solve () {
	CLR ( vis , 0 ) ;
	FOR ( i , 1 , n ) scanf ( a[i] ) ;
	FOR ( i , 1 , n ) if ( !vis[i] ) {
		int now = i ;
		top = 0 ;
		while ( !vis[now] ) S[top ++] = now , vis[now] = 1 , now = a[now] ;
		printf ( "(" ) ;
		REP ( j , 0 , top - 1 ) printf ( "%d " , S[j] ) ;
		printf ( "%d)" , S[top - 1] ) ;
	}
	printf ( "\n" ) ;
}

int main () {
	while ( ~scanf ( "%d" , &n ) ) solve () ;
	return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: