#include<bits/stdc++.h> #define ll long long #define scan(i) scanf("%d",&i) #define scand(i) scanf("%lf",&i) #define scanl(i) scanf("%lld",&i) #define f(i,a,b) for(int i=a;i<=b;i++) #define pb(i) push_back(i) #define ppb pop_back() #define pf printf using namespace std; int t,n,k; int m[102][102]; set<int> color; set<int> no; int curcolor; bool edge[102][102]; bool vis[102];//女孩有主否 int col[102]; bool find(int x){//x能否找到伴侣 f(i,1,n){ if(edge[x][i]==true&&vis[i]==false){ vis[i]=true; if(col[i]==0||find(col[i])==true){ col[i]=x; return true; } } } return false; } int maxmatch(){//返回二分图匹配最大数 memset(col,0,sizeof(col)); int ans=0; f(i,1,n){ memset(vis,0,sizeof(vis)); if(find(i)) ans++; } return ans; } int main(){ while(scanf("%d%d",&n,&k)!=EOF){ if(n==0&&k==0) return 0; no.clear(); f(i,1,n){ f(j,1,n) scan(m[i][j]),color.insert(m[i][j]); } if(k>=n){ pf("-1\n"); continue; } auto point=color.begin(); while(point!=color.end()){ memset(edge,0,sizeof(edge)); curcolor=*point; point++; f(i,1,n){ f(j,1,n){ if(m[i][j]==curcolor){ edge[i][j]=true; } } } if(maxmatch()>k) no.insert(curcolor); } if(no.empty()){ pf("-1\n"); continue; } else{ auto it=no.begin(); pf("%d",*it); it++; for(;it!=no.end();it++){ pf(" %d",*it); } putchar('\n'); } //pf("Case #%d: %lld\n",kk,ans); } return 0; }
二分图匹配问题,我以HDU balloon 1498为例