传送门:【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 ;
}