# ACdream-1030-Matching

Description

Compute the maximum cardinality matching of given undirected graph G

.

Input

The first line contains one integer n

, which denotes the number of nodes.

The following n

lines denotes the adjacency matrix A of graph G

.

(1n100

, Ai,j{0,1},Ai,i=0,Ai,j=Aj,i

)

Output

The only integer equals to the size of maximum cardinality matching.

Sample Input

3
0 1 1
1 0 1
1 1 0

Sample Output

1

Hint

One of possible matchings is to pair node 1

and 2, leaving node 3 alone.

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=105;
int g[maxn][maxn],match[maxn*maxn];
bool vis[maxn];
int n;

bool dfs(int u)
{
vis[u]=true;
for(int i=1;i<=n;i++)
{
if(g[u][i]==0||vis[i]) continue;
vis[i]=true;
if(match[i]==-1||dfs(match[i]))
{
match[i]=u;
match[u]=i;
return true;
}
}
return false;
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&g[i][j]);
int ans=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
{
if(match[i]==-1)
{
memset(vis,false,sizeof(vis));
if(dfs(i))
{
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}