代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C/C++
】
最大团(n小于64)(faster)
作者:
Parker
/ 发布于
2016/3/17
/
687
/** * WishingBone's ACM/ICPC Routine Library * * maximum clique solver */ #include <vector> using std::vector; // clique solver calculates both size and consitution of maximum clique // uses bit operation to accelerate searching // graph size limit is 63, the graph should be undirected // can optimize to calculate on each component, and sort on vertex degrees // can be used to solve maximum independent set class clique { public: static const long long ONE = 1; static const long long MASK = (1 << 21) - 1; char* bits; int n, size, cmax[63]; long long mask[63], cons; // initiate lookup table clique() { bits = new char[1 << 21]; bits[0] = 0; for (int i = 1; i < 1 << 21; ++i) bits[i] = bits[i >> 1] + (i & 1); } ~clique() { delete bits; } // search routine bool search(int step, int size, long long more, long long con); // solve maximum clique and return size int sizeClique(vector<vector<int> >& mat); // solve maximum clique and return constitution vector<int> consClique(vector<vector<int> >& mat); }; // search routine // step is node id, size is current solution, more is available mask, cons is constitution mask bool clique::search(int step, int size, long long more, long long cons) { if (step >= n) { // a new solution reached this->size = size; this->cons = cons; return true; } long long now = ONE << step; if ((now & more) > 0) { long long next = more & mask[step]; if (size + bits[next & MASK] + bits[(next >> 21) & MASK] + bits[next >> 42] >= this->size && size + cmax[step] > this->size) { // the current node is in the clique if (search(step + 1, size + 1, next, cons | now)) return true; } } long long next = more & ~now; if (size + bits[next & MASK] + bits[(next >> 21) & MASK] + bits[next >> 42] > this->size) { // the current node is not in the clique if (search(step + 1, size, next, cons)) return true; } return false; } // solve maximum clique and return size int clique::sizeClique(vector<vector<int> >& mat) { n = mat.size(); // generate mask vectors for (int i = 0; i < n; ++i) { mask[i] = 0; for (int j = 0; j < n; ++j) if (mat[i][j] > 0) mask[i] |= ONE << j; } size = 0; for (int i = n - 1; i >= 0; --i) { search(i + 1, 1, mask[i], ONE << i); cmax[i] = size; } return size; } // solve maximum clique and return constitution // calls sizeClique and restore cons vector<int> clique::consClique(vector<vector<int> >& mat) { sizeClique(mat); vector<int> ret; for (int i = 0; i < n; ++i) if ((cons & (ONE << i)) > 0) ret.push_back(i); return ret; }
试试其它关键字
同语言下
.
C分鱼问题
.
链表
.
最大连续和
.
编码字符串
.
libiconv字符编码处理及判断字符串是否为utf8
.
一组数中两两二元组,差最大有几对,差最小呢?(数组
.
通过管道获取一个进程的执行状态
.
多关键字排序
.
字符串字典序排序
.
3元一次方程(牛顿迭代法求方程的根)
可能有用的
.
C分鱼问题
.
链表
.
最大连续和
.
编码字符串
.
libiconv字符编码处理及判断字符串是否为utf8
.
一组数中两两二元组,差最大有几对,差最小呢?(数组
.
通过管道获取一个进程的执行状态
.
多关键字排序
.
字符串字典序排序
.
3元一次方程(牛顿迭代法求方程的根)
Parker
贡献的其它代码
(
3
)
.
最大团(n小于64)(faster)
.
为GridView表格添加光标提示
.
通过dblink抽取数据处理
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3