Hdu 1498 (Hungarian Algorithm bipartite +)

Hdu 1498

 

(1) meaning of the questions:

N * n have the form of the above different colors balloon, the balloon is the range of colors [1,50]

Every time a person to break the balloons, each can or find a balloon along the line, and the total destruction of all the same color balloons.

A person may operate k times, most asked what color balloons will be left behind.

 

(2) ideas:

Consider the location where the sum of each color, a color is determined to be completely deleted the minimum number of times num,

Then determine the number of times a certain color to be deleted if more than k, it means that the color will not be deleted.

 

(3) Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
const int maxn = 1e4+10;
int vis[maxn],pre[maxn],head[maxn],tot,n,k,Ans[maxn],bj[maxn];
struct Node{
	int v,nxt;
}cur[maxn];
vector <pair <int,int> > vc[55];
void Init(){
	memset(head,-1,sizeof(head));
	tot = 0;
}
void Add(int x,int y){
	cur[tot].nxt = head[x];
	cur[tot].v = y;
	head[x] = tot++;
}
bool dfs(int x){
	for(int i=head[x];i!=-1;i=cur[i].nxt){
		int y = cur[i].v;
		if(vis[y]==0){
			vis[y] = 1;
			if(pre[y]==-1||dfs(pre[y])){
				pre[y] = x;
				return true;
			}
		}
	}
	return false;
}
int fun(){
	int ans = 0;
	memset(pre,-1,sizeof(pre));
	for(int i=1;i<=n;i++){
		memset(vis,0,sizeof(vis));
		if(dfs(i)) ans++;
	}
	return ans;
}
int main(void){
	while(~scanf("%d%d",&n,&k)&&(n+k)){
		for(int i=1;i<=50;i++){
			vc[i].clear();bj[i] = 0;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int x;scanf("%d",&x);
				bj[x] = 1;
				vc[x].push_back(make_pair(i,j));
			}
		}
		int tt = 0;
		for(int i=1;i<=50;i++)
		if(bj[i]==1&&vc[i].size()>0){
			Init();
			int len = vc[i].size();
			for(int j=0;j<len;j++){
				pair <int,int> tp = vc[i][j];
				Add(tp.first,tp.second);
			}
			int num = fun();
			if(num>k){
				Ans[++tt] = i;
			}
		}
		if(tt==0) printf("-1\n");
		else{
			for(int i=1;i<=tt;i++){
				if(i>1) printf(" ");
				printf("%d",Ans[i]);
			}
			printf("\n");
		}
	}
	
	return 0;
}

 

Guess you like

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