Each lattice has balloons of different colors expressed by different numbers. Each time a row or column is selected to stamp the balloon. Each person has K chances. Find which balloons cannot be penetrated within k chances. Output the balloon numbers in ascending order. Analysis: Match rows and columns. Each color of the balloon must be determined. Therefore, a balloon number is added when the dfs parameter is passed. Feeling: 1. I thought I was going to sort by the maximum number of matches in ascending order. I was depressed when I spent one afternoon in wa yesterday. Today, I want to review the question by id. 2. I learned how to use vector. It was never used before... This is summarized and then... Code:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; struct node { int id; int cnt; }t[55]; int g[110][110]; int match[110]; int vis[110]; int n; vector<int> myv; bool cmp(node a,node b) { return a.cnt>b.cnt; } bool dfs(int u,int v) { for(int i=0;i<n;i++) { if(g[u][i]==v && !vis[i]) { vis[i]=true; if(match[i]==-1||dfs(match[i],v)) { match[i]=u; return true; } } } return false; } int main() { int k,i,p,flag,j; while(scanf("%d%d",&n,&k)&&n&&k) { flag=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) scanf("%d",&g[i][j]); } memset(t,0,sizeof(t)); for(p=1;p<=50;p++) { memset(match,-1,sizeof(match)); t[p].id=p; for(i=0;i<n;i++) { memset(vis,0,sizeof(vis)); if(dfs(i,p)) t[p].cnt++; } } sort(t+1,t+51,cmp); myv.clear(); for(j=1;j<=50;j++) { if(t[1].cnt<=k) break; flag=1; if(t[j].cnt>k) myv.push_back(t[j].id); } sort(myv.begin(),myv.end()); if(flag) { for(i=0;i<myv.size();i++) printf("%d%c",myv[i],i==myv.size()-1?'\n':' '); } if(!flag) printf("-1\n"); } return 0; }
Accumulation: sorts elements in a vector: header file # include <algorithm> vector <int> arr; // input data sort (arr. begin (), arr. end ());