Hdu 3605 (+ bipartite graph multiple matches)

Hdu 3605

 

(1) meaning of the questions:

Can live on a planet several individuals, each person is given the planet you want to select, and then gives the number of each planet up to accommodate.

 

(2) ideas:

Multiple matches bipartite graph, a maximum number of people the planet if the number is less than the number of two-dimensional array to store each planet can be used,

We can live on this planet, otherwise the back path before the modification can find the path before there are other circumstances.

 

(3) Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
int mp[maxn][12],vis[12],link[12][maxn],m,n,val[12],size[12];
bool dfs(int x){
	for(int i=1;i<=m;i++)
	if(mp[x][i]==1&&vis[i]==0){
		vis[i] = 1;
		if(size[i]<val[i]){
			link[i][++size[i]] = x;
			return true;
		}
		for(int j=1;j<=size[i];j++)
		if(dfs(link[i][j])){
			link[i][j] = x;
			return true;
		}
	}
	return false;
}
int main(void){
	while(~scanf("%d%d",&n,&m)){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&mp[i][j]);
			}
		}
		memset(size,0,sizeof(size));
		for(int i=1;i<=m;i++) scanf("%d",&val[i]);
		int fg = 0;
		for(int i=1;i<=n;i++){
			memset(vis,0,sizeof(vis));
			if(!dfs(i)){
				fg = 1;break;
			}
		}
		if(!fg) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41829060/article/details/92596083