还是回溯,但是需要注意的是因为起点不同能够导致结果不同,所以每个点都要作为起点然后枚举,找到最大的,输出就O了 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std ; const int maxn = 26 ; int g[maxn][maxn] , vis[maxn][maxn] ; int n , m , ans , cnt ; void init() { memset( g , 0 , sizeof( g ) ) ; memset( vis , 0 , sizeof( vis ) ) ; ans = 0 ; } void input() { for( int i = 0 ; i < m ; ++i ) { int u , v ; cin >> u >> v ; g[u][v] = g[v][u] = 1 ; } } void dfs( int u ) { int v ; for( v = 0 ; v < n ; ++v ) if( g[u][v] && !vis[u][v] && ! vis[v][u] ) { vis[u][v] = vis[v][u] = 1 ; cnt++ ; dfs( v ) ; vis[u][v] = vis[v][u] = 0 ; cnt-- ; } if( v >= n-1 && cnt > ans ) ans = cnt ; return ; } void output() { cout << ans << endl ; } int main() { while( cin >> n >> m && n && m ) { init() ; input() ; for( int i = 0 ; i < n ; ++i ) { cnt = 0 ; dfs( i ) ; } output() ; } return 0 ; }