首页 > 代码库 > poj1422最大独立点集合

poj1422最大独立点集合

二分图:

最大独立点集 = 顶点 - 最大匹配

#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include<math.h>using namespace std;const int maxn=222;int Map[maxn][maxn];int link[maxn];int used[maxn];int n;int dfs(int x){    for(int i=1;i<=n;i++){        if(!used[i]&&Map[x][i]){            used[i]=1;            if(link[i]==-1||dfs(link[i])){                link[i]=x;return 1;            }        }    }    return 0;}void gao(){    memset(link,-1,sizeof(link));    int ans=0;    for(int i=1;i<=n;i++){        memset(used,0,sizeof(used));        if(dfs(i)) ans++;    }    cout<<n-ans<<endl;}int main(){    int Icase;    int m;    scanf("%d",&Icase);    while(Icase--){        scanf("%d",&n);        scanf("%d",&m);        memset(Map,0,sizeof(Map));        for(int i=0;i<m;i++){            int a;int b;            scanf("%d%d",&a,&b);            Map[a][b]=1;        }        gao();    }    return 0;}