HDU 1530 最大团问题

基本上没有用上什么优化, 只是简单地搜索

代码
1 #include <iostream>
2 #include <cstdio>
3 #include <string>
4 #include <cstring>
5 #include <algorithm>
6 #include <vector>
7 #include <map>
8
9  using namespace std;
10
11 const int MAXN=60;
12 int N;
13 int Path[MAXN][MAXN];
14 int Set[MAXN];//用以寻找一个可能集合
15 int Ans;
16
17 //判断新点是否与原集合里的点互相连通
18 bool is_clique( const int end, const int point )
19 {
20 for( int i=1; i<end; ++i )
21 {
22 if( !Path[ Set[i] ][point] )
23 {
24 return false;
25 }
26 }
27 return true;
28 }
29 //递归查找,查找算法简单明了,不再详说
30 void dfs( int depth, int now )
31 {
32 if( depth+N-now+1 <= Ans )
33 {
34 return;
35 }
36 for( int i=now; i<=N; ++i )
37 {
38 if( is_clique(depth+1, i) )
39 {
40 Set[depth+1]=i;
41 dfs( depth+1, i+1 );
42 }
43 }
44 if( depth > Ans )
45 {
46 Ans=depth;
47 }
48 }
49 int main()
50 {
51 freopen("in","r",stdin);
52
53 while( scanf("%d", &N)!=EOF && N )
54 {
55 for( int i=1; i<=N; ++i )
56 {
57 for( int j=1; j<=N; ++j )
58 {
59 scanf("%d", &Path[i][j]);
60 }
61 }
62 Ans=0;
63 dfs( 0, 1 );
64 printf("%d\n", Ans);
65 }
66 return 0;
67 }
68 //还可以设置一个DP[MAXN],用以记录i~N点集合里最大团数,可以利用这一点剪枝.
69
posted on 2010-11-08 17:24  Kenfly  阅读(749)  评论(1编辑  收藏  举报