poj 1422 最小路径覆盖

来源:互联网 发布:手机淘宝模板制作软件 编辑:程序博客网 时间:2024/06/17 19:00
//可转换为二分匹配#include <iostream>#include <cstring>using namespace std;#define MAXN 122int T;int n,m;int G[MAXN][MAXN];int cx[MAXN],cy[MAXN];int sx[MAXN],sy[MAXN];int path(int u){  sx[u] = 1;  for(int v = 1; v <= n;v++)    {      if(G[u][v] > 0 && (!sy[v])){  sy[v] = 1;  if(!cy[v] || path(cy[v]))    {      cx[u] = v;      cy[v] = u;      return 1;    }}    }  return 0;}int MaxMatch(){  int res = 0;  memset(cx,0,sizeof(cx));  memset(cy,0,sizeof(cy));  for(int i = 1; i <= n; i++)    {      if(!cx[i]){  memset(sx,0,sizeof(sx));  memset(sy,0,sizeof(sy));  res += path(i);}    }  return res;}int main(){  cin>>T;  while(T--)    {      memset(G,0,sizeof(G));      cin>>n>>m;      while(m--){  int x,y;  cin>>x>>y;  G[x][y] = 1;}      cout<<n - MaxMatch()<<endl;    }  return 0;}

原创粉丝点击