1 #include <iostream> 2 #define MAXN 105 3 #define max _max 4 using namespace std; 5 6 int j; 7 bool _m[MAXN][MAXN]; 8 int ans[MAXN]; 9 int ok[MAXN]; 10 int mark[MAXN]; 11 int max; 12 int tem; 13 14 int n; 15 int k; 16 int index; 17 18 void DFS(int time); 19 int main() 20 { 21 //freopen("acm.acm","r",stdin); 22 int test; 23 int i; 24 int j; 25 int u; 26 int v; 27 cin>>test; 28 while(test --) 29 { 30 cin>>n>>k; 31 memset(mark,0,sizeof(mark)); 32 memset(_m,false,sizeof(_m)); 33 for(i = 0; i < k; ++ i) 34 { 35 cin>>u; 36 cin>>v; 37 -- u; 38 -- v; 39 _m[u][v] = true; 40 _m[v][u] = true; 41 } 42 max = -1; 43 index = 0; 44 DFS(0); 45 cout<<max<<endl; 46 for(i = 0; i < max; ++ i) 47 { 48 cout<<ok[i]+1<<" "; 49 } 50 cout<<endl; 51 } 52 } 53 //1黑 2白 54 void DFS(int time) 55 { 56 57 int i; 58 int f = 0; 59 60 for(i = 0; i < n; ++ i) 61 { 62 if(_m[time][i]) 63 { 64 if(mark[i] == 1) 65 { 66 f = 1; 67 break; 68 } 69 } 70 } 71 72 // if(tem + n - time < max) 73 // return; 74 if(time == n) 75 { 76 if(tem > max) 77 { 78 max = tem; 79 for(i = 0; i < index; ++ i) 80 { 81 ok[i] = ans[i]; 82 } 83 } 84 return; 85 } 86 87 if(f != 1) 88 { 89 mark[time] = 1; 90 ans[index ++] = time; 91 ++ tem; 92 DFS(time+1); 93 mark[time] = 0; 94 -- index; 95 -- tem; 96 } 97 if(tem + n - time > max) 98 DFS(time+1); 99 100 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com