poj2724(*二分图最大独立集)
2017-02-08 10:11
232 查看
/* translation: 给出一台机器的操作序列。每次的操作都将产生若干结果。比如011操作产生结果011,*11操作产生结果011 和结果111...以此类推。现在给出若干个操作序列。求最少能用多少个操作完成这些结果。 solution: 二分图最大独立集合 实际上就是求二分图最大独立集合,因为*能够同时当作01用,所以操作中应该尽量用*合并两个结果。所以 首先对结果进行处理。将只有1位不同的结果连起来,表示这两个结果可以用一个操作合并。那么结果就是 求最大独立集合了。因为独立集中任意两个点都是不可能合并的。所以最大独立集合中点数就是答案。 note: * 没能正确建立这道题模型,没看出是求最大独立集。 */ #include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <cmath> using namespace std; const int maxn = 20000; int n, m, V; vector<int> node; vector<int> G[maxn]; int match[maxn]; bool used[maxn]; bool dfs(int v) { used[v] = true; for(int i = 0; i < G[v].size(); i++) { int u = G[v][i], w = match[u]; if(w < 0 || !used[w] && dfs(w)) { match[v] = u; match[u] = v; return true; } } return false; } int hangary() { int res = 0; memset(match, -1, sizeof(match)); for(int v = 0; v < V; v++) { if(match[v] < 0) { memset(used, 0, sizeof(used)); if(dfs(v)) res++; } } return res; } void toInteger(char* t, int& res0, int& res1) { res0 = res1 = 0; for(int i = n-1; i >= 0; i--) { if(t[i] != '0') { res1 += (int)pow(2, n-1-i); if(t[i] == '1') res0 += (int)pow(2, n-i-1); } } } int main() { //freopen("in.txt", "r", stdin); while(~scanf("%d%d", &n, &m)) { if(n + m == 0) break; for(int i = 0; i < maxn; i++) G[i].clear(); char tmp[15]; node.clear(); int res0, res1; for(int i = 0; i < m; i++) { scanf("%s", tmp); //printf("#%s:\n", tmp); toInteger(tmp, res0, res1); //printf("%d %d\n", res0, res1); if(res0 == res1) node.push_back(res0); else { node.push_back(res0); node.push_back(res1); } } sort(node.begin(), node.end()); V = unique(node.begin(),node.end()) - node.begin(); //for(int i = 0; i < V; i++) printf("%d ", node[i]); printf("\n"); //printf("%d\n", 1 ^ 5); for(int i = 0; i < V; i++) { for(int j = 0; j < V; j++) if(i != j) { if(__builtin_popcount(node[i] ^ node[j]) == 1) { G[i].push_back(j); //printf("add edge from %d to %d\n", i, j); } } } printf("%d\n", V - hangary()); } return 0; }
相关文章推荐
- redis开启和禁用登陆密码校验
- 数据链路层
- 关于带参数模糊查询的方法 like --转载自 寂寞沙洲(博客园)
- linux下mysql相关操作
- 【BUG】ExtJS3.4.0的Grid中垂直滚动条导致列错位的解决方法
- 端口号被占用
- 【SDOI2009】HH的项链
- 网络传输速率单位
- 02-ZYNQ学习(逻辑篇)之FPGA LED控制实验
- Rxjava简单例子
- 解决多线程代码中的11个常见的问题
- Java 引用类型转换
- Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter
- Java二维码的一些简介、功能
- 循环链表(约瑟夫环问题)
- Linux 命令集合
- python的内存管理机制
- tera term 串口工具
- 2017,抓住机会,撸起袖子加油干!
- 爱人如己