Description
Input
Output
Sample Input
3 3 4 1 2 1 3 2 1 2 2 3 3 4 1 2 1 3 2 1 3 2
Sample Output
Board 1 have 0 important blanks for 2 chessmen.Board 2 have 3 important blanks for 3 chessmen.
#include <iostream> #include <string.h> #include <stdio.h> const int maxn=105; using namespace std; int G[maxn][maxn]; int linker[maxn]; bool used[maxn]; int x[maxn],y[maxn]; int n; void init() { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) G[i][j]=0; } void add_edge(int u,int v) { G[u][v]=1; } bool dfs(int u) { for(int i=1;i<=n;i++) { int v=G[u][i]; if(v==1&&!used[i]) { used[i]=true; if(linker[i]==-1||dfs(linker[i])) { linker[i]=u; return true; } } } return false; } int solve() { int ans=0; memset(linker,-1,sizeof(linker)); for(int i=1;i<=n;i++) { memset(used,false,sizeof(used)); if(dfs(i)) ans++; } return ans; } int main() { int m,p; int t=1; while(scanf("%d%d%d",&n,&m,&p)!=-1) { init(); for(int i=0;i<p;i++) { scanf("%d%d",&x[i],&y[i]); add_edge(x[i],y[i]); } int imp=0; int temp=solve(); for(int i=0;i<p;i++) { G[x[i]][y[i]]=0; if(temp!=solve()) imp++; G[x[i]][y[i]]=1; } printf("Board %d have %d important blanks for %d chessmen.\n",t++,imp,temp); } return 0; }