建行连向列的单向边,跑最大匹配
#include<iostream> #include<cstdio> #define ri register int #define u long long namespace opt { inline u in() { u x(0),f(1); char s(getchar()); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=(x<<1)+(x<<3)+s-'0'; s=getchar(); } return x*f; } } using opt::in; #define NN 205 #include<cstring> namespace mainstay { u N,M; u h[NN],cnt; struct node { u to,next; } a[NN*NN*2]; u b[NN][NN]; inline void add(const u &x,const u &y) { a[++cnt].next=h[x],a[cnt].to=y,h[x]=cnt; } u vt[NN],to[NN]; u dfs(const u &x) { for(ri i(h[x]); i; i=a[i].next) { u _y(a[i].to); if(!vt[_y]) { vt[_y]=1; if(!to[_y]||dfs(to[_y])) { to[_y]=x; return 1; } } } return 0; } inline void solve() { N=in(),M=in(); u K(in()); for(ri i(1); i<=K; ++i) { u _a(in()),_b(in()); b[_a][_b]=1; } for(ri i(1); i<=N; ++i) { for(ri j(1); j<=M; ++j) { if(!b[i][j]) { add(i,j);//单向 } } } u ans(0); for(ri i(1); i<=N; ++i) { std::memset(vt,0,sizeof(vt)); if(dfs(i)) ++ans; } printf("%d",ans); } } int main() { //freopen("x.txt","r",stdin); std::ios::sync_with_stdio(false); mainstay::solve(); }