TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5004 | Accepted: 1444 |
Description
One day, Mike's machine was infected. When Mike found out, he had already done some operations and the cheeses operated by this infected machine were infected too. He cleaned his machine as quickly as he could, and now he needs to clean the infected cheeses with the minimum number of operations. If a cheese is infected, cleaning this cheese with the machine one or more times will make this cheese free from virus again; but if a cheese is not infected, operation on this cheese will make it go bad.
Now given the infected operations Mike has done, you need to find out the minimum number of operations that must be performed to clean all the infected cheeses without making any clean cheese go bad.
Input
Output
Sample Input
3 3
*01
100
011
0 0
Sample Output
Source
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a))
using namespace std; vector<int> G[2200];
int match[2200],used[2200];
int g,b,m,cnt,l,r;
int mp[2200],posi[2200]; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int w=match[v];
if(w<0||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;
return true;
}
}
return false;
} int bipartite_match()
{
MM(match,-1);
int res=0;
for(int i=1;i<=1000+r;i++)
if(match[i]<0)
{
memset(used,0,sizeof(used));
if(dfs(i)) res++;
}
return res;
} bool onlyonedifer(int i,int j)
{
int c=(i^j);
return (c&&((c&(c-1))==0));
}//处理i,j,两个数字二进制表示只有一位不同模板 bool jione(int i)
{
int j=0;
while(i)
{
if(i&1) j++;
i>>=1;
}
return j%2==1;
} void build()
{
l=0;r=0;
for(int i=1;i<=cnt;i++)
{ if(jione(mp[i]))
{
l++;
posi[l]=mp[i];
}
else
{
r++;
posi[1000+r]=mp[i];
}
} for(int i=1;i<=1000+r;i++) G[i].clear(); for(int i=1;i<=l;i++)
for(int j=1;j<=r;j++)
if(onlyonedifer(posi[i],posi[j+1000]))
add_edge(i,j+1000);
} char s[15];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m)&&(n||m))
{
cnt=0;
MM(mp,0);
for(int i=1;i<=m;i++)
{
scanf("%s",s);
int flag=0;
++cnt;
for(int j=0;j<n;j++)
{
if(s[j]=='*')
{
flag=j+1;
mp[cnt]=(mp[cnt]<<1)+0;
}
else
mp[cnt]=(mp[cnt]<<1)+s[j]-'0';
}
if(flag)
{
mp[cnt+1]=(mp[cnt]|(1<<(n-1-(flag-1))));
++cnt;
}
}
sort(mp+1,mp+cnt+1);//unique处理前先排序
cnt=unique(mp+1,mp+cnt+1)-(mp+1);//去重
build();
int w=bipartite_match();
printf("%d\n",w+cnt-2*w);
}
return 0;
}
分析:细节很多的一道题;
1.处理两个数字二进制表示只有一位不同的模板,见代码
2.出现*时,进行二分匹配,建图很有技巧,将1出现个数为奇数次的放左边,偶数次的放右边
3.要进行去重处理
4.&运算符的优先级很低
TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想的更多相关文章
- TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9754 Accepted: 3618 Desc ...
- HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...
- poj 2060 Taxi Cab Scheme (二分匹配)
Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5710 Accepted: 2393 D ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- POJ 1466 大学谈恋爱 二分匹配变形 最大独立集
Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 11694 Accepted: 5230 D ...
- POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- hdu3715 二分+2sat+建图
题意: 给你一个递归公式,每多一层就多一个限制,问你最多能递归多少层. 思路: 先分析每一层的限制 x[a[i]] + x[b[i]] != c[i],这里面x[] = 0,1, ...
随机推荐
- windows下命令行利器---Cmder(安装,中文乱码,配置右键菜单)
很多人都是在win下开发的,这样就会出现,经常需要命令行操作,而win cmd命令和linux命令有很大差异,导致大家很难受,今天给大家介绍一个win下命令行的利器-Cmder 一.先看一下它的容颜 ...
- 爬取百度贴吧前1000页内容(requests库面向对象思想实现)
此程序以李毅吧为例子,以面向对象的设计思想实现爬取保存网页数据,暂时并未用到并发处理,以后有机会的话会加以改善 首先去百度贴吧分析贴吧地址栏中url后的参数,找到分页对应的参数pn,贴吧名字对应的参数 ...
- spark教程(10)-sparkSQL
sparkSQL 的由来 我们知道最初的计算框架叫 mapreduce,他的缺点是计算速度慢,还有一个就是代码比较麻烦,所以有了 hive: hive 是把类 sql 的语句转换成 mapreduce ...
- 屏幕的遮挡层,js得到屏幕宽高、页面宽高 (window.screen.availHeight)等--
window.screen.availWidth 返回当前屏幕宽度(空白空间) ------当手机有输入法的时候,要注意................window.screen.availHeigh ...
- vue记录错误和警告日志
https://blog.csdn.net/lucky___star/article/details/95491657 https://blog.csdn.net/weixin_34204057/ar ...
- 一文搞懂网络知识,IP、子网掩码、网关、DNS、端口号
网络的基本概念 客户端:应用 C/S(客户端/服务器) B/S(浏览器/服务器) 服务器:为客户端提供服务.数据.资源的机器 请求:客户端向服务器索取数据 响应:服务器对客户端请求作出反应,一般是返回 ...
- java常用的加密技术
详见:https://blog.csdn.net/it_beecoder/article/details/71480770 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以 ...
- gitlab和jenkins的安装及使用
gitlab 准备: 最少4G内存 先安装docker软件包然后使用docker search gitlab 查找镜像然后使用docker pull 镜像名:标签名 下载镜像启动容器: docker ...
- 递推DP(至少和至多之间的转换
UVa 10328 - Coin Toss 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 转换成抛N次至多连续有N个减去抛N次至多连续有K-1个1的情况 dp[i][k]表 ...
- MST-kruskal ElogE+V
hdu 1233 #include<stdio.h> #include<algorithm> using namespace std; struct dis { int a, ...