题意:

  给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. 用户 IIS APPPOOL\DefaultAppPool 登录失败的解决方法

    解决方案  1.先查看你的网站使用的是哪个应用程序池 2.设置应用程序池的选项 3.标识选项 选LocalSystem或NetworkService就可以解决问题

  2. tomcat 解析(一)-文件解析

    做web项目,最常用的服务器就是Apache的tomcat.虽然一直在用tomcat,但都是仅限在使用的阶段,一直没有深入学习过.想深入学习tomcat,首推的肯定是官网:http://tomcat. ...

  3. java调用dll文件中的类型转换

    char *转String   (env)->NewStringUTF("the content you want to type in"); char *转jbyteArr ...

  4. Appium —— desired_capabilities详解

    Appium 服务关键字 关键字 描述 实例 automationName 你想使用的自动化测试引擎 Appium (默认) 或 Selendroid platformName 你要测试的手机操作系统 ...

  5. FileOutputStream和FileInputStream

    package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...

  6. C#中dynamic的正确用法 以及 typeof(DynamicSample).GetMethod("Add");

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  7. hdu2544 最短路

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路径DIJKSTRA #include<stdio.h> #include<m ...

  8. python自定义函数在Python解释器中调用

    https://docs.python.org/2.7/tutorial/modules.html Modules If you quit from the Python interpreter an ...

  9. [GCJ]Password Attacker

    https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...

  10. 关于NGUI制作图集在低内存设备上的注意事项

    正在写一个游戏.由于2D且比较简单.打算用NGUI全权搞定,对,游戏内容也用NGUI. 想的很好,做的很爽.PC上跑起来happy. 天杀的诺基亚出了个手机叫lumia520,可用内存512M.单个程 ...