同Hdu 2119 Matrix.
CODE:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 510
#define MAXM 10010
struct Edge
{
int v, next;
}edge[MAXM];
int n, m;
int cnt;
int first[MAXN], link[MAXN];
bool vis[MAXN];
void init()
{
cnt = 0;
memset(first, -1, sizeof(first));
memset(link, -1, sizeof(link));
}
void read_graph(int u, int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u]; first[u] = cnt++;
}
void read_graph2()
{
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
read_graph(u, v);
}
}
int ED(int u)
{
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(!vis[v])
{
vis[v] = true;
if(link[v] == -1 || ED(link[v]))
{
link[v] = u;
return true;
}
}
}
return false;
}
void solve()
{
int ans = 0;
for(int i = 1; i <= n; i++)
{
memset(vis, false, sizeof(vis));
if(ED(i)) ans++;
}
printf("%d\n", ans);
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
read_graph2();
solve();
}
return 0;
}
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 510
#define MAXM 10010
struct Edge
{
int v, next;
}edge[MAXM];
int n, m;
int cnt;
int first[MAXN], link[MAXN];
bool vis[MAXN];
void init()
{
cnt = 0;
memset(first, -1, sizeof(first));
memset(link, -1, sizeof(link));
}
void read_graph(int u, int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u]; first[u] = cnt++;
}
void read_graph2()
{
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
read_graph(u, v);
}
}
int ED(int u)
{
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(!vis[v])
{
vis[v] = true;
if(link[v] == -1 || ED(link[v]))
{
link[v] = u;
return true;
}
}
}
return false;
}
void solve()
{
int ans = 0;
for(int i = 1; i <= n; i++)
{
memset(vis, false, sizeof(vis));
if(ED(i)) ans++;
}
printf("%d\n", ans);
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
read_graph2();
solve();
}
return 0;
}