您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: