[ZJOI 2009] 假期的宿舍
2024-08-30 21:27:08
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1433
[算法]
二分图匹配
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 55 struct edge
{
int to,nxt;
} e[MAXN * MAXN]; int i,j,n,T,tot;
int a[MAXN],b[MAXN],head[MAXN],match[MAXN << ];
bool visited[MAXN << ];
bool g[MAXN][MAXN];
bool ans; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-') f = -f;
}
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline bool hungary(int u)
{
int i,v;
visited[u] = true;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (!visited[v])
{
visited[v] = true;
if (!match[v] || hungary(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
} int main()
{ read(T);
while (T--)
{
read(n);
tot = ;
for (i = ; i <= n; i++)
{
head[i] = ;
match[i] = match[n + i] = ;
}
for (i = ; i <= n; i++) read(a[i]);
for (i = ; i <= n; i++) read(b[i]);
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
read(g[i][j]);
}
}
for (i = ; i <= n; i++)
{
if (a[i] == )
{
if (b[i] == ) continue;
for (j = ; j <= n; j++)
{
if ((i == j) || (a[j] == && g[i][j]))
addedge(i,j + n);
}
} else
{
for (j = ; j <= n; j++)
{
if (a[j] == && g[i][j])
addedge(i,j + n);
}
}
}
ans = true;
for (i = ; i <= n; i++)
{
if ((a[i] == && b[i] == ) || a[i] == )
{
memset(visited,false,sizeof(visited));
if (!hungary(i))
{
ans = false;
break;
}
}
}
if (ans) printf("^_^\n");
else printf("T_T\n");
} return ; }
最新文章
- css随笔记
- 从零开始山寨Caffe&#183;零:必先利其器
- Java 集合 - HashSet
- Xcode6如何自己添加pch文件?
- jenkins配置自动发送邮件
- [转]Spring 注解总结
- java获取手机号归属地
- Linux学习笔记(11)软件包管理
- SQL Server 2005导入Excel表问题
- 【转】C# 解析 json
- LA 2038
- mysql教程-触发器
- C 循环链表
- Html5实现头像上传和编辑,保存为Base64的图片过程
- casio 手表北京维修网络
- TCP服务端开发为例--web开发不同url请求走不同control方法
- php date函数
- GO语言初探
- 下载android4.4.2源码全过程(附已下载的源码)
- spring揭秘 读书笔记 六 bean的一生