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 ...
随机推荐
- android菜鸟学习笔记11----Intent的两点补充
关于Intent的两点补充: 1.隐式Intent启动组件,会有一个Intent解析的过程,若找不到能够处理该Intent的组件,程序就会异常终止.一个合理的做法是,在使用Intent实例启动组件如: ...
- 九度OJ 1025:最大报销额 (01背包、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4352 解决:1055 题目描述: 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C ...
- MarkdownPad - The Markdown Editor for Windows http://markdownpad.com/
MarkdownPad - The Markdown Editor for Windows http://markdownpad.com/
- vsftp时间差8个小时的解决方法
$ vi /etc/vsftpd/vsftpd.conf use_localtime=YES ;
- 流畅的python学习笔记第八章:深拷贝,浅拷贝,可变参数
首先来看赋值,浅拷贝,深拷贝. 一赋值: a=['word',2,3] b=a print id(a),id(b) print [id(x) for x in a] print [id(x) for ...
- python3使用pdfminer3k解析pdf文件
安装pdfminer模块 pip3 install pdfminer3k 代码如下 #!/usr/bin/env python # coding:utf8 # author:Z time:2018/7 ...
- Machine Learning No.3: Logistic Regression
1. Decision boundary when hθ(x) > 0, g(z) = 1; when hθ(x) < 0, g(z) = 0. so the hyppthesis is: ...
- spring-boot4代码
App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...
- SDUT OJ 之 连通分量个数 (dfs)
数据结构实验:连通分量个数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在无向图中,如果从顶点vi到顶点vj有路径,则称vi ...
- CSS3实现水位充满文字特效
CSS3实现水位充满文字特效是一款既是Loading特效也是文字特效,Loading动画开始时,文字中的水位渐渐上升,为了模拟水位上升的真实效果,水面还会波浪浮动,当Loading动画结束时,文字中的 ...