DFS 10.1.5.253 1501





#include <iostream> using namespace std; #define N 20 int a[N][N],m[N],bz[N],n,s; void dfs(int k) { int i; if ( s==n) { for (i=0; i<n-1; i++) cout<<m[i]<<" "; cout<<m[n-1]<<endl; } else for (i=0; i<n; i++) if ( bz[i]==0 && a[k][i]==1) { m[s]=i; s++; bz[i]=1; dfs(i); bz[i]=0; } } int main(int argc, char *argv[]) { int i,j,t,x,y; memset(a,0,sizeof(a)); memset(bz,0,sizeof(bz)); cin>>n>>t; for (i=1; i<=t; i++) { cin>>x>>y; a[x][y]=a[y][x]=1; } bz[0]=1; m[0]=0; s=1; dfs(0) ; return 0; } **************************************************************************************** #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<cmath> #include<map> #include<stdio.h> #include<cstring> //#define DEBUG using namespace std; vector<int>ve[1005]; int d[1005],flag; void DFS(int k) { if(flag==0){ cout<<k; flag=1; } else cout<<" "<<k; d[k]=1; for(int i=0;i<ve[k].size();++i) { if(d[ve[k][i]]==0)DFS(ve[k][i]); } } int main() { //#ifndef ONLINE_JUDGE #ifdef DEBUG freopen("in.txt","r",stdin); #endif int n,m,a,b; while(cin>>n>>m) { for(int i=0;i<n;++i)ve[i].clear(); for(int i=0;i<m;++i) { cin>>a>>b; ve[a].push_back(b); ve[b].push_back(a); } for(int i=0;i<n;++i)sort(ve[i].begin(),ve[i].end()); flag=0; memset(d,0,sizeof(d)); DFS(0); } //#ifndef ONLINE_JUDGE #ifdef DEBUG freopen("con","r",stdin); system("pause"); #endif return 0; } ************************************************************************************8 #include <iostream> using namespace std; #define N 20 int a[N][N],m[N],bz[N],n,s; void dfs(int k) { int i; if ( s==n) { for (i=0; i<n-1; i++) cout<<m[i]<<" "; cout<<m[n-1]<<endl; } else for (i=0; i<n; i++) if ( bz[i]==0 && a[k][i]==1) { m[s]=i; s++; bz[i]=1; dfs(i); bz[i]=0; } } int main(int argc, char *argv[]) { int i,j,t,x,y; memset(a,0,sizeof(a)); memset(bz,0,sizeof(bz)); cin>>n>>t; for (i=1; i<=t; i++) { cin>>x>>y; a[x][y]=a[y][x]=1; } bz[0]=1; m[0]=0; s=1; dfs(0) ; return 0; }

  

posted @ 2013-12-06 13:45  博园少主  阅读(230)  评论(0编辑  收藏  举报