用染色法判断是不是二分图,然后再输出最大匹配(据说风神做法是并查集= =,)
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
#define N 205
vector <int> v[N];
vector <int> vv[N];
int link[N];
int vis[N];
int col[N];
int a[N], b[N];
queue<int> q;
int n, m;
bool ok;
void init()
{
for( int i = 1; i <= n; i++ )
{
v[i].clear();
vv[i].clear();
}
memset(col, -1, sizeof(col));
ok = 0;
while( !q.empty() )
q.pop();
}
void bfs()
{
for( int i = 1; i <= n; i++ )
q.push(i);
while( !q.empty() )
{
int now = q.front();
q.pop();
//if( col[now] != -1 )
// continue;
for( int i = 0; i < vv[now].size(); i++ )
{
int to = vv[now][i];
if( col[to] != -1 )
{
if(col[to] == col[now] )
{
ok = 1;
return;
}
else
continue;
}
else
{
col[to] = col[now] == 1? 0: 1;
q.push(to);
}
}
}
}
int dfs( int u )
{
for( int i = 0; i < v[u].size(); i++ )
{
int to = v[u][i];
if( !vis[to] )
{
vis[to] = 1;
if( link[to] == -1 || dfs( link[to] ) )
{
link[to] = u;
return 1;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
int z, x;
for( int i = 1; i <= m; i++ )
{
scanf("%d%d", &z, &x);
v[z].push_back(x);
vv[z].push_back(x);
vv[x].push_back(z);
}
bfs();
if( ok )
{
printf("No\n");
continue;
}
memset(link, -1, sizeof(link));
int ans = 0;
for( int i = 1; i <= n; i++ )
{
memset(vis, 0, sizeof(vis));
ans += dfs(i);
}
printf("%d\n", ans);
}
return 0;
}