Help Twilight Sparkle find the lexicographic smallest solution. (Only considering numbers).
题目就是要求i σ(i) σ(σ(i))....选过了就不能重选,遍历一遍就OK 了
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=100010;
int a[N],vis[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=1;i<=n;i++)
{
if(vis[i])
continue;
printf("(");
int t=i;
int first=1;
while(!vis[t])
{
vis[t]=1;
if(first) first=0;
else printf(" ");
printf("%d",t);
t=a[t];
}
printf(")");
}
printf("\n");
}
return 0;
}