UVa 1663 Purifying Machine (二分匹配)
题意:每一个01串中最多含有一个‘*’,‘*’既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串。
如:000、010、0*1表示000、010、001、011,最少只需用00*、01*这两个即可表示出来。
析:因为最多只有一个星,所以每个串最多能代表两个串,所以就是要两两匹配的尽量多,也就是二分匹配喽,要注意,给的串可能会有重复的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<string> v;
int V;
vector<int> G[maxn];
int match[maxn];
bool used[maxn]; void addEdge(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
} 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 solve(){
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;
} bool judge(int i, int j){
int cnt = 0, k = 0;
while(k < n){
if(v[i][k] != v[j][k]) ++cnt;
++k;
}
return cnt == 1;
} int main(){
while(cin >> n >> m && m+n){
v.clear();
for(int i = 0; i < m ; ++i){
string s;
cin >> s;
if(s.find('*') != string::npos){
int pos = s.find('*');
s[pos] = '1';
v.push_back(s);
s[pos] = '0';
}
v.push_back(s);
}
sort(v.begin(), v.end());
V = unique(v.begin(), v.end()) - v.begin();
for(int i = 0; i < V; ++i) G[i].clear();
for(int i = 0; i < V; ++i)
for(int j = i+1; j < V; ++j)
if(judge(i, j)) addEdge(i, j);
printf("%d\n", V - solve());
}
return 0;
}
UVa 1663 Purifying Machine (二分匹配)的更多相关文章
- UVA 1663 Purifying Machine (二分图匹配,最大流)
		题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ... 
- POJ 2724 Purifying Machine (二分图匹配)
		题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ... 
- UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)
		题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ... 
- UVA - 12083 Guardian of Decency (二分匹配)
		题意:有N个人,已知身高.性别.音乐.运动.要求选出尽可能多的人,使这些人两两之间至少满足下列四个条件之一. 1.身高差>40 2.性别相同 3.音乐不同 4.运动相同 分析: 1.很显然 ... 
- TTTTTTTTTTTTTTTTTT POJ  2724   奶酪消毒机    二分匹配  建图 比较难想
		Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5004 Accepted: 1444 ... 
- HDU - 1045   Fire Net(二分匹配)
		Description Suppose that we have a square city with straight streets. A map of a city is a square bo ... 
- zoj 1002 Fire Net (二分匹配)
		Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ... 
- hdu-1045.fire net(缩点 + 二分匹配)
		Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ... 
- hdu 1045 Fire Net(二分匹配 or 暴搜)
		Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ... 
随机推荐
- gitPermission denied (publickey).
			$ git clone git@github.com:DavidWanderer/test1.git Cloning into 'test1'... Warning: Permanently adde ... 
- OpenCV Machine Learning  之  正态贝叶斯分类器  (Normal  Bayes  Classifier)
			版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhjm07054115/article/details/27631913 
- Spring自定义配置--ConfigurationProperties
			自定义配置的变量名: 在 *.properties 里面定义特定的变量 server.port=9000 amazon.associateId=habuma-20 建立Properties文件制定特定 ... 
- 编写你的第一个django应用程序2
			从1停止的地方开始,我们将设置数据库,创建您的第一个模型,并快速介绍django自动生成的管理站点 数据库设置 现在,打开mysite/settings.py.这是一个普通的python模块,其中模块 ... 
- [2017-12-20]ElasticSearch 小记
			介绍 ElasticSearch是一款搜索引擎中间件,因其强大的全文索引.查询统计能力和非常方便的全套基于Restful的接口,以及在自动分片.无停机升级扩容.故障转移等运维方面的高效性,逐渐成为中小 ... 
- koa-bodyparser返回413状态码的问题
			413 Request Entity Too Large(请求实体太大) 数日前,我用 node.js 写的一个日志服务抛出了这个状态码-- 自己写的服务抛出了一个自己都不认识的状态码,这是最气的!( ... 
- ScrollView当显示超出当前页面时自动移动到最底端【转】
			本文转载自:http://gundumw100.iteye.com/blog/1162964 卷轴视图(ScrollView)是指当拥有很多内容,一屏显示不完时,需要通过滚动来显示视图.比如在做一个阅 ... 
- python当前工作文件夹中创建空的.txt文件
			import os def new_txt(): a1='实线' b = os.getcwd() + '\\fazhandadao_test_txt\\' if not os.path.exists( ... 
- 苹果AppStore如何申请加急审核
			登录iTunesconnect,点击右上角的“?”图标,选择“联系我们”. iTunes Connect首页 依次选择“App Review”.“App Store Review” .” Reques ... 
- BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】
			题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2020 题意: FJ开车去买K份食物. 如果他的车上有X份食物,每走一里就花费X元. FJ的 ... 
