POJ 1129
#include<iostream> #include<stdio.h> #include<string> #define MAXN 60 using namespace std; int max_clique(int n, int * * mat, int *ret) ; int main() { //freopen("acm.acm","r",stdin); int i; int ans; int j; string s; int num; int * * _m; int * ret; while(cin>>num) { if(num == 0) break; _m = new int * [num]; ret = new int[num]; for(i = 0; i < num; ++ i) { _m[i] = new int[num]; memset(_m[i],0,sizeof(int)*num); } //for(i = 0; i < num; ++ i) // for(j = 0; j < num; ++ j) // cout<<_m[i][j]<<" "; for(i = 0; i < num; ++ i) { cin>>s; if(s.length() == 2) continue; else { for(j = 2; j < s.length(); ++ j) { _m[s[0] - 'A'][s[j] - 'A'] = 1; } } } ans = max_clique(num,_m,ret); cout<<ans; if(ans == 1) cout<<" channel needed."<<endl; else cout<<" channels needed."<<endl; delete [] _m; delete ret; } } void clique(int n, int* u, int * * mat, int size, int& max, int& bb, int * res, int* rr, int* c) { int i, j, vn, v[MAXN]; if (n) { if (size + c[u[0]] <= max) return; for (i = 0; i < n + size - max && i < n; ++ i) { for (j = i + 1, vn = 0; j < n; ++ j) if (mat[u[i]][u[j]]) v[vn ++] = u[j]; rr[size] = u[i]; clique(vn, v, mat, size + 1, max, bb, res, rr, c); if (bb) return; } } else if (size > max) { max = size; for (i = 0; i < size; ++ i) res[i] = rr[i]; bb = 1; } } int max_clique(int n, int * *mat, int * ret) { int max = 0, bb, c[MAXN], i, j; int vn, v[MAXN], rr[MAXN]; for (c[i = n - 1] = 0; i >= 0; -- i) { for (vn = 0, j = i + 1; j < n; ++ j) if (mat[i][j]) v[vn ++] = j; bb = 0; rr[0] = i; clique(vn, v, mat, 1, max, bb, ret, rr, c); c[i] = max; } return max; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com