Description

I am the bone of my sword. Steel is my body, and the fire is my blood.
- from Fate / Stay Night
You must have known the legend of King Arthur and his knights of the round table. The round table has no head, implying that everyone has equal status. Some knights are close friends with each other, so they prefer to sit next to each other.

Given the relationship of these knights, the King Arthur request you to find an arrangement such that, for every knight, his two adjacent knights are both his close friends. And you should note that because the knights are very united, everyone has at least half of the group as his close friends. More specifically speaking, if there are N knights in total, every knight has at least (N + 1) / 2 other knights as his close friends.
 

Input

The first line of each test case contains two integers N (3 <= N <= 150) and M, indicating that there are N knights and M relationships in total. Then M lines followed, each of which contains two integers ai and bi (1 <= ai, bi <= n, ai != bi), indicating that knight ai and knight bi are close friends.
 

Output

For each test case, output one line containing N integers X1, X2, ..., XN separated by spaces, which indicating an round table arrangement. Please note that XN and X1 are also considered adjacent. The answer may be not unique, and any correct answer will be OK. If there is no solution exists, just output "no solution".
 

Sample Input

3 3 1 2 2 3 1 3 4 4 1 4 2 4 2 3 1 3
 

Sample Output

1 2 3 1 4 2 3
 


超级简单

    #include<stdio.h>
    #include<string.h>
    #define M 407
using namespace std;
    int ans[M],g[M][M];
    bool map[M][M],vis[M];
    int n,m;

    void init()
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                if(i==j)
                    g[i][j]=0;
                else
                    g[i][j]=0;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
    }

    void revese(int ans[M],int s,int t)//将ans数组中s到t的部分倒置
    {
        int temp;
        while(s<t)
        {
            temp=ans[s];
            ans[s]=ans[t];
            ans[t]=temp;
            s++;
            t--;
        }
    }

    void Hamilton()
    {
        int s=1,t;//初始化s取1号点
        int ansi=2;
        int i,j,w,temp;
        for(i=1;i<=n;i++)
            if(g[s][i])break;
        t=i;//取任意连接s的点为t
        vis[s]=vis[i]=true;
        ans[0]=s;
        ans[1]=t;
        while(true)
        {
            while(true)//从t向外扩展
            {
                for(i=1;i<=n;i++)
                    if(g[t][i]&&!vis[i])
                    {
                        ans[ansi++]=i;
                        vis[i]=true;
                        t=i;
                        break;
                    }
                if(i>n)break;
            }
            //将当前得到的序列倒置,s和t互换,从t继续扩展,相当于在原来的序列上从s扩展
            w=ansi-1;
            i=0;
            revese(ans,i,w);
            temp=s;
            s=t;
            t=temp;
            //从新的t向外扩展,相当于在原来的序列上从s向外扩展
            while(true)
            {
                for(i=1;i<=n;i++)
                    if(g[t][i]&&!vis[i])
                    {
                        ans[ansi++]=i;
                        vis[i]=true;
                        t=i;
                        break;
                    }
                if(i>n)break;
            }
            //如果s和t不相邻,进行调整
            if(!g[s][t])
            {
                for(i=1;i<ansi-2;i++)
                    if(g[ans[i]][t]&&g[s][ans[i+1]])//取序列中一点i,使得ans[i]与t相连接且ans[i+1]与s相连
                        break;
                //将从ans[i+1]到t部分的ans[]倒置
                w=ansi-1;
                i++;
                t=ans[i];
                revese(ans,i,w);
            }
            //如果当前s和t相连
            if(ansi==n)return;//如果当前序列中包含n个元素,算法结束
            //当前序列中的元素个数小于n,寻找点ans[i],使得ans[i]与ans[]外一点相连
            for(j=1;j<=n;j++)
            {
                if(vis[j])continue;
                for(i=1;i<ansi-2;i++)
                    if(g[ans[i]][j])
                        break;
                if(g[ans[i]][j])
                    break;
            }
            s=ans[i-1];
            t=j;
            revese(ans,0,i-1);//将ans[]中s到ans[i-1]部分的ans[]倒置
            revese(ans,i,ansi-1);//将ans[]中ans[i]到t的部分倒置
            ans[ansi++]=j;//将点j加入到ans[]的尾部
            vis[j]=true;
        }
    }

    int main()
    {
       // freopen("cin.txt","r",stdin);
        while(~scanf("%d%d",&n,&m))
        {
         //   n*=2;
            init();
            int a,b;
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&a,&b);
                g[a][b]=g[b][a]=1;//巧妙的建立反图
            }
            Hamilton();
            printf("%d",ans[0]);
            for(int i=1;i<n;i++)
                printf(" %d",ans[i]);
            printf("\n");
        }
        return 0;
    }