按要求连边,跑匈牙利
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> using namespace std; #define N 110 int T,n; int sc[N],go[N]; int a[N][N]; int ly[N],vis[N]; struct edge { int to,next; }e[20010]; int head[N]; int cnt; int getint() { int w=0,q=0; char c=getchar(); while ((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if (c==‘-‘) q=1,c=getchar(); while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w; } void link(int x,int y) { e[++cnt]=(edge){y,head[x]}; head[x]=cnt; } bool find(int x) { for (int i=head[x];i;i=e[i].next) { int t=e[i].to; if (!vis[t]) { vis[t]=1; if (!ly[t] || find(ly[t])) { ly[t]=x; return true; } } } return false; } int main() { T=getint(); while (T--) { n=getint(); for (int i=1;i<=n*2;i++) head[i]=0,ly[i]=0; cnt=0; for (int i=1;i<=n;i++) sc[i]=getint(); for (int i=1;i<=n;i++) { go[i]=getint(); if (sc[i] && !go[i]) link(i,n+i); } for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) a[i][j]=getint(); if (!sc[i] || (sc[i] && !go[i])) { for (int j=1;j<=n;j++) if (a[i][j] && sc[j]) link(i,j+n); } } int f=1; for (int i=1;i<=n;i++) if (!sc[i] || (sc[i] && !go[i])) { for (int j=1;j<=n*2;j++) vis[j]=0; if (!find(i)) { f=0; break; } } if (f) puts("^_^"); else puts("T_T"); } return 0; }
时间: 2024-10-12 12:01:24