题意:

  给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1。模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己。问:模板串可以缩减为几个,同样可以匹配原来m个串同样能匹配的所有串。

思路:

  差点想不出是二分图匹配了。

  将原来m个串所能匹配的串给取出来放到集合中(记得去重),编上号。并为他们黑白着色,源点到白色点有容量1的边,黑色点到汇点有容量为1的边,对于该白色点所能匹配的所有黑色点,都有一条容量为1的边。跑一次最大流,得知匹配对数,这些匹配的都只用1个模板串,不匹配的独用一个模板串。

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <set>
#include <algorithm>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
int n, m, up;
char s[N][];
int col[N*], path[N*], flow[N*], edge_cnt;
vector<int> vect[N*];
vector<int> vec[N*]; struct node
{
int from, to, cap, flow;
node(){};
node(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){};
}edge[]; void add_node(int from,int to,int cap,int flow)
{
edge[edge_cnt]=node(from,to,cap,flow);
vect[from].push_back(edge_cnt++);
} bool ismatch(string &a,string &b) //只有一个位不同即为匹配
{
int cnt=;
for(int i=; i<n; i++)
if( a[i]!=b[i] ) cnt++;
if(cnt==) return true;
else return false;
} void color(int s,int c)
{
col[s]=c;
for(int i=; i<vec[s].size(); i++)
{
int t=vec[s][i];
if(!col[t]) color(t,-col[s]);
}
} set<string> sett;
string str[N*];
int build_graph()
{
//把包含*号的拆成两个数字,再转int,可能有重复。
sett.clear();
for(int i=; i<m; i++)
{
int j=;
for(; j<n; j++)
{
if(s[i][j]=='*')
{
s[i][j]='';
sett.insert( string(s[i]) ); //重复的自动去掉
s[i][j]='';
sett.insert( string(s[i]) );
break;
}
}
if(j==n) sett.insert( string(s[i]) );
} up=;
for(set<string>::iterator it=sett.begin(); it!=sett.end(); it++)
str[up++]=*it; for(int i=; i<=up+; i++) vect[i].clear(),vec[i].clear();//第二个vec是为了着色用的 for(int i=; i<up; i++) //匹配建无向图,着色用
{
for(int j=i+; j<up; j++)
if(ismatch(str[i],str[j]))
{
vec[i].push_back(j);
vec[j].push_back(i);
}
} //黑白着色
memset(col,,sizeof(col));
for(int i=; i<up; i++) if(!col[i]) color(i,); //添加源点and汇点,重新建图
memset(edge,,sizeof(edge));
edge_cnt=;
for(int i=; i<up; i++)
{
if(col[i]==) //0是源点
{
add_node(,i,,);
add_node(i,,,);
for(int j=; j<vec[i].size(); j++) //相邻的点颜色不同
{
int q=vec[i][j];
add_node(i,q,,);
add_node(q,i,,);
}
}
if(col[i]==) //up是汇点
{
add_node(i,up,,);
add_node(up,i,,);
}
}
} int BFS(int s,int e)
{
deque<int> que(,s);
flow[s]=INF;
while(!que.empty())
{
int x=que.front(); que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(e.cap>e.flow && !flow[e.to])
{
path[e.to]=vect[x][i];
flow[e.to]=min(flow[e.from], e.cap-e.flow);
que.push_back(e.to);
}
}
if(flow[e]) break;
}
return flow[e];
} int max_flow(int s,int e)
{
int ans_flow=;
while(true)
{
memset(flow,,sizeof(flow));
memset(path,,sizeof(path));
int tmp=BFS(s,e);
if(!tmp) return ans_flow;
ans_flow+=tmp; int ed=e;
while(ed!=s)
{
int t=path[ed];
edge[t].flow+=tmp;
edge[t^].flow-=tmp;
ed=edge[t].from;
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
char c;
while(scanf("%d%d",&n,&m), n+m)
{
for(int i=; i<m; i++)//输入
{
for(int j=; j<n; )
{
c=getchar();
if(c=='*' || c=='' || c=='') s[i][j++]=c;
}
s[i][n]='\0';
}
build_graph();//建图
printf("%d\n",up--max_flow(,up));
}
return ;
}

AC代码

UVA 1663 Purifying Machine (二分图匹配,最大流)的更多相关文章

  1. UVa 1663 Purifying Machine (二分匹配)

    题意:每一个01串中最多含有一个‘*’,‘*’既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串. 如:000.010.0*1表示000.010.001.011,最 ...

  2. POJ 2724 Purifying Machine (二分图匹配)

    题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...

  3. 最短路&生成树&二分图匹配&费用流问题

    最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...

  4. HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. cogs_14_搭配飞行员_(二分图匹配+最大流,网络流24题#01)

    描述 http://cojs.tk/cogs/problem/problem.php?pid=14 有一些正飞行员和副飞行员,给出每个正飞行员可以和哪些副飞行员一起飞.一架飞机上必须一正一副,求最多多 ...

  6. BZOJ 3546 Life of the Party (二分图匹配-最大流)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3546 题意:给定一个二分图.(AB两个集合的点为n,m),边有K个.问去掉哪些点后 ...

  7. luogu P3386 【模板】二分图匹配

    二次联通门 : luogu P3386 [模板]二分图匹配 /* luogu P3386 [模板]二分图匹配 最大流 设置源点,汇点,连到每条边上 跑一边最大流即可 */ #include <i ...

  8. UVa 二分图匹配 Examples

    这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...

  9. 【最大流,二分图匹配】【hdu2063】【过山车】

    题意:裸的求二分图匹配 建立一个源点 连向一边所有的点 容量为1; 另外一边点都连向汇点  容量为1; 二分图的边容量也为1 源点汇点求一遍最大流即可 #include <cstdio> ...

随机推荐

  1. SQL SERVER开窗函数

    作为一名开发人员来讲,我感觉在职场白混了好多年,可能是自己真的没有进取的精神吧,看了<程序员的SQL金典>这本电子书,真的让我学到了不少知识,真心喜欢这本电子书,书中讲解的内容比较好懂,也 ...

  2. Spring3+hibernate4+struts2整合的 过程中发生如下错误

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...

  3. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  4. 剑指offer--面试题10--相关

    题目一:判断一个整数是不是2的n次幂 实现大概如下: int main() { ; )) == ) //重要!! std::cout<<"YES!"<<st ...

  5. jquery的一些用法

    一.选择器 单选按钮:$(this).find(".answer").find("input[name='answer_" + id + "']:ch ...

  6. epoll 知识总结

    poll/select/epoll 对比 http://www.cnblogs.com/apprentice89/p/3234677.html    ---有待继续学习 http://blog.chi ...

  7. VS2010 创建WindowsService服务

    1.新建一个Windows 服务 2.添加Installer 这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装. 在VS中添加Installer 右 ...

  8. Using command-line Subversion to access project source files

    Help index About source code version control with Software Configuration Management (Subversion) Usi ...

  9. What is the difference between database table and database view?

    The database table has a physical existence in the database. A view is a virtual table, that is one ...

  10. Oracle中关于数据库实例名与数据库服务名(转载)

    今天同事,出现了数据库连接失败的问题,一起百度了一下,结果总算解决了,以下是一些转载过来的普及知识. 1.查询数据库名:select name,dbid from v$database;或者命令行:s ...