#include<cstdio>#include<vector>constint maxn =1005;int n, m, t;int mch[maxn], vistime[maxn];
std::vector<int> e[maxn];booldfs(constint u,constint tag);intmain(){scanf("%d %d %d",&n,&m,&t);for(int u, v; t;--t){scanf("%d %d",&u,&v);
e[u].push_back(v);}int ans =0;for(int i =1; i <= n;++i)if(dfs(i, i)){++ans;}printf("%d\n", ans);}booldfs(constint u,constint tag){if(vistime[u]== tag)returnfalse;
vistime[u]= tag;for(auto v : e[u])if((mch[v]==0)||dfs(mch[v], tag)){
mch[v]= u;returntrue;}returnfalse;}
洛谷P1129 [ZJOI2007] 矩阵游戏
代码:
#include<iostream>#include<cstdio>#include<cstring>usingnamespace std;constint N =40005;int h[N], ne[N *2], to[N *2];int tot, T, n;voidadd(int x,int y){
ne[++tot]= h[x];
h[x]= tot, to[tot]= y;}intread(void){int x =0;char c =getchar();while(!isdigit(c)) c =getchar();while(isdigit(c)){
x =(x <<3)+(x <<1)+ c -'0';
c =getchar();}return x;}int tim, vis[N], match[N];intdfs(int x){for(int i = h[x]; i; i = ne[i]){int y = to[i];if(vis[y]== tim)continue;
vis[y]= tim;if(!match[y]||dfs(match[y])){
match[y]= x;return1;}}return0;}voidpre(void){memset(h,0,sizeof(h));memset(ne,0,sizeof(ne));memset(to,0,sizeof(to));memset(match,0,sizeof(match));
tot =0;}intmain(){
T =read();while(T--){pre();
n =read();for(int i =1;i <= n; i++)for(int j =1;j <= n; j++){int x =read();if(x){add(i, j + n);add(j + n, i);}}int ans =0;for(int i =1;i <= n; i++)
tim++, ans +=dfs(i);if(ans == n)printf("Yes\n");elseprintf("No\n");}return0;}