The Accomodation of Students HDU - 2444

邻接表

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 16007
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=3e5;
bool vis[N];
int link[N];
int e[N],ne[N],h[N],idx; 
void add(int a,int b)
{
	e[idx]=b;
	ne[idx]=h[a];
	h[a]=idx++;
}
int color[N];
vector<int> G[N];
bool dfs(int x){
    for(int i=h[x];i!=-1;i=ne[i]){
        int y=e[i];
        if(!vis[y]){
            vis[y]=true;
            if(link[y]==-1 || dfs(link[y])){
                link[y]=x;
                return true;
            }
        }
    }
    return false;
}
bool bipartite(int x){
    for(int i=h[x];i!=-1;i=ne[i]){
        int y=e[i];
        if(color[y]==color[x])
            return false;
        else if(color[y]==0){
            color[y]=3-color[x];
            if(!bipartite(y))
                return false;
        }
    }
    return true;
}
int hungarian(int n)
{
    memset(color,0,sizeof(color));
    for(int i=1;i<=n;i++){
        if(!color[i]){
            color[i]=1;
            if(!bipartite(i))
                return -1;
        }
    }
 
 
    int ans=0;
    for(int i=1;i<=n;i++){
        memset(vis,false,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
    	idx=0;
    	memset(h,-1,sizeof h);
        memset(link,-1,sizeof(link));
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x); 
        }
        int res=hungarian(n);
        if(res==-1)
            printf("No\n");
        else
            printf("%d\n",res/2);
    }
    return 0;
}

vector实现矩阵

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 16007
#define INF 0x3f3f3f3f
#define N 10001
#define LL long long
using namespace std;
bool vis[N];
int link[N];
int color[N];
vector<int> G[N];
bool dfs(int x){
    for(int i=0;i<G[x].size();i++){
        int y=G[x][i];
        if(!vis[y]){
            vis[y]=true;
            if(link[y]==-1 || dfs(link[y])){
                link[y]=x;
                return true;
            }
        }
    }
    return false;
}
bool bipartite(int x){
    for(int i=0;i<G[x].size();i++){
        int y=G[x][i];
        if(color[y]==color[x])
            return false;
        else if(color[y]==0){
            color[y]=3-color[x];
            if(!bipartite(y))
                return false;
        }
    }
    return true;
}
int hungarian(int n)
{
    memset(color,0,sizeof(color));
    for(int i=1;i<=n;i++){
        if(!color[i]){
            color[i]=1;
            if(!bipartite(i))
                return -1;
        }
    }
 
 
    int ans=0;
    for(int i=1;i<=n;i++){
        memset(vis,false,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
        memset(link,-1,sizeof(link));
        for(int i=0;i<N;i++)
            G[i].clear();
 
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
 
        int res=hungarian(n);
        if(res==-1)
            printf("No\n");
        else
            printf("%d\n",res/2);
    }
    return 0;
}
posted @ 2020-03-04 15:27  晴屿  阅读(102)  评论(0编辑  收藏  举报