Description
n
个节点
Input
一个整数
Output
如果有解则输出该哈密顿回路,否则输出 −1
Sample Input
2
Sample Output
0 1 0
Solution
n
为奇数时,到
n
为偶数时,由于第
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
int n,vis[maxn],ans[maxn],res;
void dfs(int u)
{
if(vis[u])return ;
vis[u]=1;
dfs(2*u%n);
dfs((2*u+1)%n);
ans[res++]=u;
}
int main()
{
scanf("%d",&n);
if(n&1)printf("-1\n");
else
{
res=0;
dfs(0);
for(int i=res-1;i>=0;i--)printf("%d ",ans[i]);
printf("0\n");
}
return 0;
}