洛谷 2055 BZOJ 1433 [ZJOI2009]假期的宿舍
【题解】
既然是一人对应一床,那么显然可以用二分图匹配来做。俩人认识的话,如果其中一个a是在校学生,另一个b不回家,b就可以向a的床连边(a,b当然也可以是同一个人)。
然后如果最大匹配数大于等于需要床的人数,就存在合法方案。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100
#define rg register
using namespace std;
int T,n,cnt,tot,time,ans,last[N],v[N],from[N];
bool sch[N],gh[N];
struct edge{
int to,pre;
}e[N*N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
int dfs(int x){
for(rg int i=last[x],to;i;i=e[i].pre)if(v[to=e[i].to]!=time){
v[to]=time;
if(!from[to]||dfs(from[to])){
from[to]=x;
return ;
}
}
return ;
}
int main(){
T=read();
while(T--){
memset(v,,sizeof(v));
memset(from,,sizeof(from));
memset(last,,sizeof(last));
ans=; cnt=; tot=; time=;
n=read();
for(rg int i=;i<=n;i++) sch[i]=read();
for(rg int i=;i<=n;i++){
int x=read();
if(sch[i]) if((gh[i]=x)==) cnt++;else;
else gh[i]=;
}
for(rg int i=;i<=n;i++)
for(rg int j=;j<=n;j++) if(read()==||i==j){
if(sch[i]&&!gh[j]) e[++tot]=(edge){i,last[j]},last[j]=tot;
if(sch[j]&&!gh[i]) e[++tot]=(edge){j,last[i]},last[i]=tot;
}
for(rg int i=;i<=n;i++) if(!gh[i])time++,ans+=dfs(i);
puts(ans>=n-cnt?"^_^":"T_T");
}
return ;
}
洛谷 2055 BZOJ 1433 [ZJOI2009]假期的宿舍的更多相关文章
- bzoj 1433: [ZJOI2009]假期的宿舍 -- 最大流
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MB Description Input Output Sample Input ...
- bzoj 1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍 Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample ...
- BZOJ 1433 [ZJOI2009]假期的宿舍(网络流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1433 [题目大意] 在校学生有自己的床,外校的则没有,如果两个同学相互认识,则可以借用 ...
- bzoj 1433: [ZJOI2009]假期的宿舍【匈牙利算法】
i能睡j床的连边(i,j),跑最大匹配或者最大流,然后看看人数能不能对上总数即可 #include<iostream> #include<cstdio> #include< ...
- 1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍 链接 分析: 直接网络流,看是否满足即可. S向每个有需要的人连边,有床的向T连边,认识的人之间互相连边. 代码: #include<cstdio&g ...
- 1433. [ZJOI2009]假期的宿舍【二分图】
Description 学校放假了······有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如A 和B都是学校的学生,A要回家,而C来看B,C与A不认识.我们假设每个人只能 ...
- BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2375 Solved: 1005[Submit][Sta ...
- bzoj1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2286 Solved: 969[Submit][Stat ...
- bzoj1433:[ZJOI2009]假期的宿舍
明显的二分图最大匹配. #include<cstdio> #include<cstring> #include<cctype> #include<algori ...
随机推荐
- 数据读进set,进行后处理
#include <iostream> #include <vector> #include <cstddef> #include <string> # ...
- libnids TCP数据流重组,显示TCP连接过程的程序总无法捕获数据包解决办法:
法一: 指定可用网卡: nids_params.device="lo"; 法二: nids.h中有这么一段: struct nids_chksum_ctl { u_int ne ...
- 可视图linux--ubuntu的安装以及tool的安装
1.下载ubuntu:http://cn.ubuntu.com/download/ 右键然后选择复制链接地址,然后将这个地址粘贴到迅雷,下载速度会比浏览器下载快. 2.创建虚拟机,安装ubuntu-- ...
- codemirror插件-文件比较组件merge
目的: 为了实现文件比较功能 引用文件 从github下载项目后,从以下路径引用文件,其中部分github分支中codemirror.js 需要运行项目,自动合成 <link rel=style ...
- bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】
设f[u][0/1]为u这个点不选/选,转移的时候从儿子转移,f[u][1]=sum(f[son][0])+1,f[u][0]=sum(max(f[son][0],f[e[i].to][1])) #i ...
- bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车【排序+贪心】
从小到大排个序,然后能选就选 #include<iostream> #include<cstdio> #include<algorithm> using names ...
- php生成唯一订单号的方法
第一种 $danhao = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); 第二种 $danhao = date('Ym ...
- macbookpro安装Ubuntu16.04.1 LTS爬坑之旅。亲测有效(集众家之长)。安装时间为2017-11-19。
1.格式化U盘 要求:(1)切换分区格式为Mac OS扩展 (日志型):(2)方案(scheme)设置为:GUID Partition Map:如图(使用mac自带磁盘工具) 2.给Ubuntu划分磁 ...
- C#常量知识整理
整数常量 整数常量可以是十进制.八进制或十六进制的常量.前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,没有前缀则表示十进制. 整数常量也可以有后缀,可以是 U 和 L 的组合,其中,U ...
- mysql若干问题
一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...