易错点:
- 判断必须写全写好.
- 需要dfs时再dfs,否则会把别人需要的床占掉.
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=2000;
struct Edge{
int from,to,nxt;
}e[MAXN];
int head[MAXN],edgeCnt=1;
void addEdge(int u,int v){
e[++edgeCnt].from=u;
e[edgeCnt].to=v;
e[edgeCnt].nxt=head[u];
head[u]=edgeCnt;
}
int vis[MAXN],match[MAXN];
bool dfs(int x){
for(int i=head[x];i;i=e[i].nxt){
int nowV=e[i].to;
if(vis[nowV])continue;
vis[nowV]=1;
if(!match[nowV]||dfs(match[nowV])){
match[nowV]=x;
return 1;
}
}
return 0;
}
void init(){
memset(match,0,sizeof(match));
memset(e,0,sizeof(0));
edgeCnt=1;
memset(head,0,sizeof(head));
}
bool isStudent[MAXN],isHome[MAXN];
int main(){
int t;
scanf("%d",&t);
while(t--){
init();
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){//isStudent
int tmp;
scanf("%d",&tmp);
if(tmp==0)isStudent[i]=0;
else if(tmp==1)isStudent[i]=1;
}
for(int i=1;i<=n;i++){
int tmp;
scanf("%d",&tmp);
if(tmp==0)isHome[i]=0;
else if(tmp==1)isHome[i]=1;
}
for(int i=1;i<=n;i++){
if(isStudent[i])addEdge(i,i+n);
for(int j=1;j<=n;j++){
int tmp;
scanf("%d",&tmp);
if(tmp==1&&isStudent[j]){//ijknow
addEdge(i,j+n);
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
if(!isStudent[i]||(isStudent[i]&&(!isHome[i]))){
memset(vis,0,sizeof(vis));
if(dfs(i))ans++;
}
}
int needNum=0;
for(int i=1;i<=n;i++){
if(!isStudent[i]||(isStudent[i]&&(!isHome[i])))needNum++;
}
if(ans>=needNum)printf("^_^\n");
else printf("T_T\n");
}
return 0;
}