UVA 1663 Purifying Machine (二分图匹配,最大流)
题意:
给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 (二分图匹配,最大流)的更多相关文章
- UVa 1663 Purifying Machine (二分匹配)
题意:每一个01串中最多含有一个‘*’,‘*’既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串. 如:000.010.0*1表示000.010.001.011,最 ...
- POJ 2724 Purifying Machine (二分图匹配)
题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...
- 最短路&生成树&二分图匹配&费用流问题
最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- cogs_14_搭配飞行员_(二分图匹配+最大流,网络流24题#01)
描述 http://cojs.tk/cogs/problem/problem.php?pid=14 有一些正飞行员和副飞行员,给出每个正飞行员可以和哪些副飞行员一起飞.一架飞机上必须一正一副,求最多多 ...
- BZOJ 3546 Life of the Party (二分图匹配-最大流)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3546 题意:给定一个二分图.(AB两个集合的点为n,m),边有K个.问去掉哪些点后 ...
- luogu P3386 【模板】二分图匹配
二次联通门 : luogu P3386 [模板]二分图匹配 /* luogu P3386 [模板]二分图匹配 最大流 设置源点,汇点,连到每条边上 跑一边最大流即可 */ #include <i ...
- UVa 二分图匹配 Examples
这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...
- 【最大流,二分图匹配】【hdu2063】【过山车】
题意:裸的求二分图匹配 建立一个源点 连向一边所有的点 容量为1; 另外一边点都连向汇点 容量为1; 二分图的边容量也为1 源点汇点求一遍最大流即可 #include <cstdio> ...
随机推荐
- iPhone 6 & iPhone 6 Plus适配
转载请注明出处: http://www.cnblogs.com/dokaygang128/p/4049461.html Apple 今年发布了两款新的iPhone机器,iPhone 6 和iPhone ...
- IOS7 适配时导航栏变黑
当适配IOS的布局时遇到问题:导航栏和菜单栏后台会变黑色. self.edgesForExtendedLayout = UIRectEdgeNone; 原因是系统默认这两个控件是半通明的. 解决方案: ...
- oracle字符集问题总结
在进行web开发和oracle安装的过程中经常有人对字符集搞不清楚,因此对此做一下总结. 1.第一个问题:字符集之间的区别是什么呢? 常见的字符集有:UTF-8和GBK (1)GBK字符集 G ...
- uva 10330 最大流
拆点 将节点 i 的容量拆成从 i 到 i+n 的边的容量 套用最大流模板 ac #include <cstdio> #include <cstdlib> #include ...
- ssh 内在溢出
相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...
- 全球说:要给 OneAlert 点100个赞
客户背景 「全球说」 Talkmate,是北京酷语时代教育科技有限公司(酷语科技)旗下产品,酷语科技是一家诞生于中国的语言技术公司,致力于为全球用户提供一个全新的多语言学习和社交网络平台 . 全球说是 ...
- dynamic介绍
Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具有类型 object 一样. 在 ...
- Secret and Whisper
这两个应用都是以匿名方式向朋友分享信息. 36氪的网友评论到: Whisper 的厉害之处在于给年轻人提供了一个释放压力的地方,比如说他去年陪孩子看病,这种经历不愿意放到 Facebook 上,但是他 ...
- java.util.concurrent包API学习笔记
newFixedThreadPool 创建一个固定大小的线程池. shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭. awaitTermination():用于等待子线程结束, ...
- SaaS系列介绍之十四: SaaS软件开发分析
1 引言 真正的问题,不是电脑是否具备思考能力,而是人类是否具备这种能力 ________B.F.Skinner<计算机科学> SaaS模式不同于传 ...