题意:给你一个矩阵,里面的数字代表气球的颜色,每次操作可以删除一行或者一列的同颜色的气球,给你操作的次数,问有多少种颜色的气球不能全部被删除,按字典序输出。
裸二分最大匹配。和POJ 3041的建图方式一样。
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include <set> #include <vector> #include <stack> #include <map> #include <iomanip> #define PI acos(-1.0) #define Max 2005 #define inf 1<<28 #define LL(x) (x<<1) #define RR(x) (x<<1|1) #define Rep(i,s,t) for(int i=(s);i<=(t);++i) #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define mp(a,b) make_pair(a,b) using namespace std; int Map[Max][Max] ; bool vis[Max] ; vector<int>g[Max] ; int link[Max] ; bool vis1[Max] ; int dfs(int x ) { int l = g[x].size() ; for (int i = 0 ; i < l ; i ++ ) { int e = g[x][i] ; if(!vis[e]) { vis[e] = 1 ; if(link[e] == -1 || dfs(link[e])) { link[e] = x ; return 1 ; } } } return 0 ; } int ans[Max] ; int main() { int n , k ; while(scanf("%d%d",&n,&k) ,(n + k )) { mem(vis1,0) ; for (int i = 0 ; i < n ; i ++ ) { for (int j = 0 ; j < n ; j ++ ) { cin >> Map[i][j] ; vis1[Map[i][j]] = 1 ; } } int nn = 0 ; for (int i = 1 ; i <= 50 ; i ++ ) { if(!vis1[i])continue ; int num = 0 ; for (int j = 0 ; j <= n ; j ++ )g[j].clear() ; mem(link,-1) ; for (int j = 0 ; j < n ; j++ ) { for (int kk = 0 ; kk < n ; kk ++ ) { if (Map[j][kk] == i) g[j].push_back(kk) ; } } for (int j = 0 ; j < n ; j ++ ) { mem(vis,0) ; num += dfs(j) ; } if(num > k) ans[nn ++] = i ; } if(!nn) cout <<-1<<endl; else { cout <<ans[0]; for (int i = 1 ; i < nn ; i ++ ) cout <<" "<<ans[i]; cout <<endl; } } return 0; }