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, ...
随机推荐
- python_0基础开始_day09
第九节 1,函数初始 s = "qwertyuiop"n = 0for i in s: n += 1print(n)lst = [1,2,3,4,5]n = 0for i ...
- Django创建数据库常用字段及参数
Django创建数据库常用字段及参数 常用字段 1.models.AutoField 自增列= int(11) 如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列 ...
- VS2017 VS2019 无法进入安装界面闪退问题(windows7SP1)
如果离线安装 Visual Studio 2017/2019出现“即将完成…一切即将准备就绪.”的画面后,等几秒安装程序没有任何错误提示就关闭了,无法继续安装. 解决方法: 将vs_enterpris ...
- webpack4 打包
1. 基本安装及命令 npm config set registry https://registry.npm.taobao.org // 淘宝镜像npm install webpack-c ...
- pytorch简单框架
网络搭建: mynn.py: import torchfrom torch import nnclass mynn(nn.Module): def __init__(self): super(mynn ...
- hadoop上hive的安装
1.前言 说明:安装hive前提是要先安装hadoop集群,并且hive只需要再hadoop的namenode节点集群里安装即可(需要再所有namenode上安装),可以不在datanode节点的机器 ...
- linux图形界面安装
1.问题现象 1.1 startx命令不可用 [root@linuxtest2 ~]# startx 1.3 init 5无法执行完成 [root@linuxtest2 ~]#init 5 2.问题原 ...
- openstack dashboard开启https
前提条件: 1.基于http的dashboard能正常访问 2.拥有ssl证书 第一步:修改/etc/openstack-dashboard/local_settings 在DEBUG = False ...
- signal,blinker:信号(看我脸色行事)
signal 什么是信号(signal)? 信号在linux中被用来进行进程间的通信和异步处理,简单地可以理解会为回调函数,当发送一个信号时,会触发相应的操作.python中的signal模块便是用来 ...
- 关于Vue-$router传参出现刷新页面或者返回页面丢失数据的问题
也算是踩到坑了,但不是我踩到的,不过还是得说下这个问题,很严重,对于小白和初学者是比较有帮助的,如果使用到路由传参,请选择你想要的传参方式params或者query 1.query this.$rou ...