#531. NOIP2010TG-27

NOIP2010TG-27

  1. (阅读程序写结果)
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE=100;
int n,m,r[SIZE];
bool map[SIZE][SIZE],found;
bool successful()
{
  int i;
  for(i=1;i<=n;i++)
    if(!map[r[i]][r[i%n+1]])
      return false;
  return true;
}
void swap(int *a,int *b)
{
  int t;
  t=*a;
  *a=*b;
  *b=t;
}
void perm(int left,int right)
{
  int i;
  if(found)
    return;
  if(left>right)
  {
    if(successful())
    {
      for(i=1;i<=n;i++)
        cout<<r[i]<<' ';
      found=true;
    }
    return;
  }
  for(i=left;i<=right;i++)
  {
    swap(r+left,r+i);
    perm(left+1,right);
    swap(r+left,r+i);
  }
}

int main()
{
  int x,y,i;
  cin>>n>>m;
  memset(map,false,sizeof(map));
  for(i=1;i<=m;i++)
  {
    cin>>x>>y;
    map[x][y]=true;
    map[y][x]=true;
  }
  for(i=1;i<=n;i++)
    r[i]=i;
  found=false;
  perm(1,n);
  if(!found)
    cout<<"No solution!"<<endl;
  return 0;
}

输入:

9 12

1 2

2 3

3 4

4 5

5 6

6 1

1 7

2 7

3 8

4 8

5 9

6 9

输出:{{ input(1) }}