极大团的资料已经在前一篇文章中说过了
下了一下官方的数据,有的数据RE了,可能是在本机上系统栈分配的太小的原因吧。
在POJ HOJ都AC了,其中一分用STL写的在 POJ TLE,数据太强了
具体思想如wiki上说的,但是要加上一个强剪枝!!
起初用自己写的bitset,简化了集合求并的操作,但是遍历复杂了,而且没有那个剪枝,所以一直TLE
代码如下:
1 /*
2 * =====================================================================================
3 *
4 * Filename: clique.cpp
5 *
6 * Description: max clique
7 *
8 * Version: 1.0
9 * Created: 2011年02月23日 15时49分48秒
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: ronaflx
14 * Company: hit-acm-group
15 *
16 * =====================================================================================
17 */
18 #include < iostream >
19 #include < sstream >
20 #include < cstring >
21 #include < cstdio >
22 #include < algorithm >
23 #include < vector >
24 using namespace std;
25 class Bron_Kerbosch
26 {
27 private :
28 const static int N = 130 ;
29 int n, maps[N][N], cnt;
30 void countClique(vector < int >& p,vector < int > & x)
31 {
32 if (p.size() == 0 && x.size() == 0 )
33 cnt ++ ;
34 for (vector < int > ::iterator i = x.begin(); i != x.end();i ++ )
35 {
36 vector < int > ::iterator j;
37 for (j = p.begin(); j != p.end() && maps[ * i][ * j]; j ++ );
38 if (j == p.end())
39 return ;
40 } // 剪枝!!
41 vector < int > tmpp, tmpx, copyx;
42 copyx.assign(x.begin(), x.end());
43 for (vector < int > ::iterator i = p.begin(); i != p.end(); i ++ )
44 {
45 tmpp.clear();
46 tmpx.clear();
47 if (i != p.end() - 1 )
48 {
49 for (vector < int > ::iterator j = i + 1 ;j != p.end();j ++ )
50 {
51 if (maps[ * i][ * j])
52 tmpp.push_back( * j);
53 }
54 }
55 for (vector < int > ::iterator j = copyx.begin();j != copyx.end();j ++ )
56 {
57 if (maps[ * i][ * j])
58 tmpx.push_back( * j);
59 }
60 countClique(tmpp, tmpx);
61 if (cnt > 1000 )
62 return ;
63 copyx.push_back( * i);
64 }
65 x.assign(copyx.begin(), copyx.end());
66 tmpp.clear();
67 tmpx.clear();
68 copyx.clear();
69 }
70 public :
71 void initialize( int n, int m)
72 {
73 memset(maps, 0 , sizeof (maps));
74 this -> n = n;
75 for ( int i = 0 ;i < m;i ++ )
76 {
77 int a, b;
78 scanf( " %d %d " , & a, & b);
79 a -- , b -- ;
80 maps[a][b] = true ;
81 maps[b][a] = true ;
82 }
83 }
84 int countClique()
85 {
86 cnt = 0 ;
87 vector < int > p, x;
88 for ( int i = 0 ;i < n;i ++ )
89 p.push_back(i);
90 countClique(p, x);
91 return cnt;
92 }
93 }one ;
94 int main()
95 {
96 int n, m;
97 while (scanf( " %d %d " , & n, & m) == 2 )
98 {
99 one.initialize(n, m);
100 int ans = one.countClique();
101 if (ans > 1000 )
102 printf( " Too many maximal sets of friends.\n " );
103 else
104 printf( " %d\n " , ans);
105 }
106 return 0 ;
107 }
不用STL的比较高效
1 #include < iostream >
2 #include < sstream >
3 #include < cstring >
4 #include < cstdio >
5 #include < algorithm >
6 using namespace std;
7 class Bron_Kerbosch
8 {
9 private :
10 const static int N = 130 ;
11 int n, maps[N][N], cnt;
12 void countClique( int * p, int ps, int * x, int xs)
13 {
14 if (ps == 0 )
15 {
16 if (xs == 0 )
17 cnt ++ ;
18 return ;
19 }
20 for ( int i = 0 ;i < xs;i ++ )
21 {
22 int j, v = x[i];
23 for (j = 0 ;j < ps && maps[p[j]][v];j ++ );
24 if (j == ps)
25 return ;
26 }
27 int tmpp[N], tmpps = 0 , tmpx[N], tmpxs = 0 ;
28 for ( int i = 0 ;i < ps;i ++ )
29 {
30 int v = p[i];
31 tmpps = tmpxs = 0 ;
32 for ( int j = i + 1 ;j < ps;j ++ )
33 {
34 int u = p[j];
35 if (maps[v][u])
36 tmpp[tmpps ++ ] = u;
37 }
38 for ( int j = 0 ;j < xs;j ++ )
39 {
40 int u = x[j];
41 if (maps[v][u])
42 tmpx[tmpxs ++ ] = u;
43 }
44 countClique(tmpp, tmpps, tmpx, tmpxs);
45 if (cnt > 1000 )
46 return ;
47 x[xs ++ ] = v;
48 }
49 }
50 public :
51 void initialize( int n, int m)
52 {
53 memset(maps, 0 , sizeof (maps));
54 this -> n = n;
55 for ( int i = 0 ;i < m;i ++ )
56 {
57 int a, b;
58 scanf( " %d %d " , & a, & b);
59 a -- , b -- ;
60 maps[a][b] = true ;
61 maps[b][a] = true ;
62 }
63 }
64 int countClique()
65 {
66 cnt = 0 ;
67 int p[N], x[N];
68 for ( int i = 0 ;i < n;i ++ )
69 p[i] = i;
70 countClique(p, n, x, 0 );
71 return cnt;
72 }
73 }one ;
74 int main()
75 {
76 int n, m;
77 while (scanf( " %d %d " , & n, & m) == 2 )
78 {
79 int a, b;
80 one.initialize(n, m);
81 int ans = one.countClique();
82 if (ans > 1000 )
83 printf( " Too many maximal sets of friends.\n " );
84 else
85 printf( " %d\n " , ans);
86 }
87 return 0 ;
88 }