1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<stack> #include<cmath> #include<set> #include<bitset> #include<complex> #include<assert.h> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge *j=h[x];j;j=j->next) #define mem(a) memset(a,0,sizeof(a)) #define ll long long #define eps 1e-8 #define succ(x) (1<<x) #define mid (x+y>>1) #define lowbit(x) (x&(-x)) #define sqr(x) ((x)*(x)) #define NM 15 #define nm 20005 using namespace std; const double pi=acos(-1); const int inf=1e9+7; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f*x; }
int n,m,_x,_y,tot; int d[NM][NM][nm],f[NM][NM][nm],g[nm]; struct tmp{int x,y,t;}pre[nm]; bool a[NM][NM];
void out(int x,int y,int t){ for(int p=f[x][y][t];__builtin_popcount(t)>1;y=p,p=f[x][y][t]){ printf("%d %d\n",p,y); if(x!=y)t^=succ(y-1); } }
int main(){ n=read();m=read(); inc(i,1,m){ _x=read();_y=read(); a[_x][_y]++;a[_y][_x]++; } tot=succ(n)-1; inc(j,0,tot)g[j]=inf; inc(p,1,n){ inc(i,1,n)inc(j,0,tot)d[p][i][j]=inf; d[p][p][succ(p-1)]=0; inc(j,0,tot)if(j&succ(p-1)){ inc(i,1,n)if(d[p][i][j]<inf&&(p!=i||j==succ(p-1))){ if(__builtin_popcount(j)>2&&a[i][p]){ if(g[j]>d[p][i][j]+1){ g[j]=d[p][i][j]+1; pre[j]=tmp{p,i,j}; } } inc(k,1,n)if(a[i][k]&&((j&succ(k-1))==0||(k==p&&__builtin_popcount(j)>2))){ if(d[p][k][j|succ(k-1)]>d[p][i][j]+1){ d[p][k][j|succ(k-1)]=d[p][i][j]+1; f[p][k][j|succ(k-1)]=i; } } } } } inc(i,0,tot)if(g[i]<inf){ inc(x,1,n)if(i&succ(x-1))inc(y,1,n)if(i&succ(y-1)){ int t=tot^i; for(int j=t;j;j=t&(j-1)){ if(g[i|j]>g[i]+d[x][y][j|succ(x-1)|succ(y-1)]){ g[i|j]=g[i]+d[x][y][j|succ(x-1)|succ(y-1)]; pre[i|j]=tmp{x,y,j|succ(x-1)|succ(y-1)}; } } } } printf("%d\n",g[tot]); while(__builtin_popcount(tot)>2){ out(pre[tot].x,pre[tot].y,pre[tot].t); tot=tot^pre[tot].t|succ(pre[tot].x-1)|succ(pre[tot].y-1); } inc(i,1,n)if(tot&succ(i-1)){ inc(j,i+1,n)if(tot&succ(j-1)){ printf("%d %d\n",i,j); break; } break; } return 0; }
|