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> ...
随机推荐
- nenu contest3
http://vjudge.net/contest/view.action?cid=55702#overview 12656 - Almost Palindrome http://uva.online ...
- plsql 使用技巧
问题1: 每次打开plsql 布局都被恢复还原了. 你可以调整好一个布局,然后window ->Save Lay out 即可
- Unity手游:自动寻路Navmesh 跳跃 攀爬 斜坡
原地址:http://dong2008hong.blog.163.com/blog/static/46968827201403114644210/ 步骤 1.在场景中摆放各种模型,包括地板,斜坡,山体 ...
- Understanding and Using Servlet Filters
Overview of How Filters Work This section provides an overview of the following topics: How the Serv ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- POJ 1701
/* Every tenant went up N floors would make the dissatisfied degree rise N * a + 0.5 * N * (N - 1) d ...
- mahout安装配置
1.下载mahout 下载地址:http://mahout.apache.org 我下载的最新版:mahout-distribution-0.9 2.把mahout解压到你想存放的文档,我是放在/Us ...
- Unrecognized Windows Sockets error: 0: JVM_Bind异常
根据端口查看 根据PID查看具体的进程 任务管理器->查看-选择列,选中PID 然后查看任务管理器.
- spring的两种属性注入方式setter注入和构造器注入或者自动注入
1.这里的属性自动注入,与注解配置bean是两回事.这里的自动注入,指的是bean属性的自动注入. bean属性自动注入,包括byNAme和ByType两码事. 2.所有的applicationCon ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...