HDU 1068 Girls and Boys 二分图

来源:互联网 发布:网络规划师不承认高工 编辑:程序博客网 时间:2024/05/18 00:18

题意:找出这个班最大的没有男女关系的集合。

思路:结果就是n二分图的最大匹配。然而因为我作死,想把这个二分图染色,然后再匹配,然后就跪了。我认为我是对的呀(QAQ)。然而还是学网上的做法,拆点,把一点拆成两个点,然而把最大匹配除以2就是原来的匹配数,这样真的好方便呀。不用染色和重新建边了。

我自己的建边有问题,我的建边是针对一棵树的,对于成环图就成了问题。

然后我就脑补了一种方法,来解决成环图的问题。就是用Map来hash每条路径,路径访问过了就不再访问了。就不会造成vis数组访问问题。

http://acm.hdu.edu.cn/showproblem.php?pid=1068

拆点版本

/*********************************************    Problem : HDU 1068     Author  : NMfloat    InkTime (c) NM . All Rights Reserved .********************************************/#include <map>#include <set>#include <queue>#include <cmath>#include <ctime>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define rep(i,a,b)  for(int i = a ; i <= b ; i ++)#define rrep(i,a,b) for(int i = b ; i >= a ; i --)#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)#define cls(a,x)   memset(a,x,sizeof(a))#define eps 1e-8using namespace std;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const int MAXN = 1e3;const int MAXE = 2e3;typedef long long LL;typedef unsigned long long ULL;struct Edge { //记录边    int to;    Edge * next;}E[MAXE],*EE,E1[MAXE];struct Gragh { //记录图的结点    Edge * first;}G[MAXN],G1[MAXN];int N,M;//二分图左右结点的个数bool visit[MAXN];int match[MAXN];//v2中匹配的情况void addedge(int u,int v) { //加边    EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;    //EE->to = u ; EE -> next = G[v].first ; G[v].first = EE ++;}int T,n,m,k;void init() {    EE = E1; N = M = 0;    cls(G1,0) ; cls(G,0) ; }bool find_path(int u) {    int v;    repE(p,u) {        v = p->to;        if(!visit[v]) {            visit[v] = 1;            if(match[v] == -1 || find_path(match[v])) {//v没有匹配或者v可以找到另一条路径                match[v] = u;                return true;            }        }    }    return false;}int Max_match() {    cls(match,-1);    int cnt = 0;    rep(i,1,N) {        cls(visit,0);        if(find_path(i)) cnt ++;    }    return cnt;}void input() {    int u,v,num;    N = n;    rep(i,1,n) {        scanf("%d: (%d)",&u,&num);        rep(j,1,num) {            scanf("%d",&v);            addedge(u+1,v+N);            //printf("%d %d\n",u,v);        }    }   }void solve() {    printf("%d\n",n-Max_match()/2);}int main(void) {    //freopen("a.in","r",stdin);    while(~scanf("%d",&n)) {        init();        input();        solve();    }    return 0;}

我的不拆点染色重新建边版本

/*********************************************    Problem : HDU 1068     Author  : NMfloat    InkTime (c) NM . All Rights Reserved .********************************************/#include <map>#include <set>#include <queue>#include <cmath>#include <ctime>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define rep(i,a,b)  for(int i = a ; i <= b ; i ++)#define rrep(i,a,b) for(int i = b ; i >= a ; i --)#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)#define cls(a,x)   memset(a,x,sizeof(a))#define eps 1e-8using namespace std;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const int MAXN = 1e4;const int MAXE = 2e4;typedef long long LL;typedef unsigned long long ULL;struct Edge { //记录边    int to;    Edge * next;}E[MAXE],*EE,E1[MAXE];struct Gragh { //记录图的结点    Edge * first;}G[MAXN],G1[MAXN];int N,M;//二分图左右结点的个数bool visit[MAXN];int match[MAXN];//v2中匹配的情况int X[MAXN];int Y[MAXN];int color[MAXN];int vis[MAXN];map<int,int>mp;void addedge(int u,int v) { //加边    EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;    //EE->to = u ; EE -> next = G[v].first ; G[v].first = EE ++;}void addedge_1(int u,int v) {    EE->to = v ; EE -> next = G1[u].first ; G1[u].first = EE ++;    //EE->to = u ; EE -> next = G[v].first ; G[v].first = EE ++;}int T,n,m,k;void init() {    EE = E1; N = M = 0;    cls(G1,0) ; cls(G,0) ; }bool find_path(int u) {    int v;    repE(p,u) {        v = p->to;        if(!visit[v]) {            visit[v] = 1;            if(match[v] == -1 || find_path(match[v])) {//v没有匹配或者v可以找到另一条路径                match[v] = u;                return true;            }        }    }    return false;}int Max_match() {    cls(match,-1);    int cnt = 0;    rep(i,1,N) {        cls(visit,0);        if(find_path(i)) cnt ++;    }    return cnt;}void dfs(int u,int color_idx) {    color[u] = color_idx % 2;    if( color[u] ) Y[u] = ++M;    else X[u] = ++N;    for(Edge * p = G1[u].first ; p ; p = p -> next) {        int v = p -> to;         if(!vis[v]) {            vis[v] = 1;            dfs(v,color_idx+1);        }    }}void dfs1(int u) {    for(Edge * p = G1[u].first ; p ; p = p -> next) {        int v = p -> to;        if(color[u] == 0) {             if(mp[X[u]+Y[v]*N] == 0) {                mp[X[u] + Y[v] * N] = 1;                addedge(X[u],Y[v]+N) ;                 dfs1(v);            }        }//printf("%d %d\n",X[u],Y[v]+N); }        else {             if(mp[X[v]+Y[u]*N] == 0) {                mp[X[v]+Y[u]*N] = 1;                addedge(X[v],Y[u]+N) ; //printf("%d %d\n",X[v],Y[u]+N); }                dfs1(v);            }        }    }}void input() {    int u,v,num;    rep(i,1,n) {        scanf("%d: (%d)",&u,&num);        rep(j,1,num) {            scanf("%d",&v);            addedge_1(u,v);            //printf("%d %d\n",u,v);        }    }   }void solve() {    // cls(X,0); cls(Y,0);    cls(vis,0);    rep(i,0,n-1) { //dfs染色以及重新编号        if(!vis[i]){            vis[i] = 1;            dfs(i,1);        }    }    EE = E;    mp.clear();    rep(i,0,n-1) {//dfs建边        dfs1(i);    }    //printf("N : %d M : %d\n",N,M);    printf("%d\n",n-Max_match());}int main(void) {    //freopen("a.in","r",stdin);    while(~scanf("%d",&n)) {        init();        input();        solve();    }    return 0;}
0 0
原创粉丝点击