HDU 1498 50 years, 50 colors (row and column matching + minimum vertex overwrite)

Source: Internet
Author: User

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 ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.