Portal
All students have beds to let people who know themselves go to bed. Students who are not in school do not occupy beds.
Consider creating a bipartite graph, matching the person's bed, and finding the maximum match. According to the question, everyone should be arranged, So there cannot be a match.
#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 55int Link[MAXN],vis[MAXN];int Graph[MAXN][MAXN];int is_out[MAXN],is_s[MAXN];int T,N;bool Hungary(int u){ for(register int i=1;i<=N;++i){ if(is_s[i]==0||vis[i]==1)continue; if(Graph[u][i]==0)continue; vis[i] = 1; if(Link[i]==0||Hungary(Link[i])){ Link[i] = u; return true; } } return false;}int main(){ scanf("%d",&T); for(register int k=1;k<=T;++k){ std::memset(Link,0,sizeof(Link)); scanf("%d",&N); for(register int i=1;i<=N;++i)scanf("%d",&is_s[i]); for(register int i=1;i<=N;++i)scanf("%d",&is_out[i]); for(register int i=1;i<=N;++i) for(register int j=1;j<=N;++j)scanf("%d",&Graph[i][j]); for(register int i=1;i<=N;++i)Graph[i][i] = 1; bool flag = true; for(register int i=1;i<=N;++i){ if(is_s[i]==1&&is_out[i]==1)continue; std::memset(vis,0,sizeof(vis)); if(!Hungary(i)){ flag = false; break; } } if(flag)puts("^_^"); else puts("T_T"); } return 0;}
[Luogu2055] [zjoi2009] dormitory for holidays