传送门
住的人是所有不回家的人和所有来看望的人xi,有的宿舍是所有本校的学生yi。
s->xi,1 yi->t,1 对于ij认识,xi->yi,1
即最大匹配。
#include
#include
#include
#include
using namespace std;
const int max_n=55;
const int max_N=max_n*2+2;
const int max_m=max_N*max_N;
const int max_e=max_m*2;
const int INF=2e9;
int T,n,N,A,B,maxflow;
int school[max_n],home[max_n],known[max_n][max_n],a[max_n],b[max_n];
int tot,point[max_N],nxt[max_e],v[max_e],remain[max_e];
int deep[max_N],cur[max_N];
queue <int> q;
inline void clear()
{
maxflow=0;
memset(school,0,sizeof(school)); memset(home,0,sizeof(home)); memset(known,0,sizeof(known));
memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); A=0; B=0;
tot=-1; memset(point,-1,sizeof(point)); memset(nxt,-1,sizeof(nxt)); memset(v,0,sizeof(v)); memset(remain,0,sizeof(remain));
memset(deep,0,sizeof(deep)); memset(cur,0,sizeof(cur));
}
inline void addedge(int x,int y,int cap)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
inline bool bfs(int s,int t)
{
memset(deep,0x7f,sizeof(deep)); deep[s]=0;
for (int i=1;i<=N;++i) cur[i]=point[i];
while (!q.empty()) q.pop(); q.push(s);
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=point[now];i!=-1;i=nxt[i])
if (deep[v[i]]>N&&remain[i])
{
deep[v[i]]=deep[now]+1;
q.push(v[i]);
}
}
return deep[t]inline int dfs(int now,int t,int limit)
{
if (now==t||!limit) return limit;
int f,flow=0;
for (int i=cur[now];i!=-1;i=nxt[i])
if (deep[v[i]]==deep[now]+1&&(f=dfs(v[i],t,min(remain[i],limit))))
{
flow+=f;
limit-=f;
remain[i]-=f;
remain[i^1]+=f;
if (!limit) break;
}
return flow;
}
inline void dinic(int s,int t)
{
while (bfs(s,t))
maxflow+=dfs(s,t,INF);
}
int main()
{
scanf("%d",&T);
while (T--)
{
clear();
scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%d",&school[i]);
for (int i=1;i<=n;++i) scanf("%d",&home[i]);
for (int i=1;i<=n;++i)
if ((school[i]&&!home[i])||!school[i])
a[++A]=i;
for (int i=1;i<=n;++i)
if (school[i])
b[++B]=i;
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
scanf("%d",&known[i][j]);
for (int i=1;i<=n;++i) known[i][i]=1;
N=A+B+2;
for (int i=1;i<=A;++i)
addedge(1,1+i,1);
for (int i=1;i<=B;++i)
addedge(1+A+i,N,1);
for (int i=1;i<=A;++i)
for (int j=1;j<=B;++j)
if (known[a[i]][b[j]])
addedge(1+i,1+A+j,1);
dinic(1,N);
if (maxflow==A) printf("^_^\n");
else printf("T_T\n");
}
}