Poj 1486 Sorting Slides (binary matching)

Source: Internet
Author: User

There are n slides of varying sizes (only the outlines and the numbers above are visible) A, B, C, D, E... Stacked in order. Now we know the size of each slide. Because the slides are transparent, we can see the numbers on the slides (given the coordinates of each number, but I don't know which slides these numbers belong to.) Now, you need to output a matched slide number and number based on the current known information. Idea: it is obvious that the problem of binary matching is to determine the matching, first find the maximum matching. Then, each edge is deleted in sequence, and the maximum matching value is obtained. If the maximum matching value is reduced, the matching value is determined.

 //168K  0MS  #include <stdio.h>  #include <string.h>  const int M = 30;    struct Slide  {      int x1,x2,y1,y2;  } slide[M];  int mat[M][M],link[M],cur[M],vis[M],n;    int DFS(int u)  {      for (int v = 1; v<=n; v ++)      {          if (!vis[v]&&mat[u][v])          {              vis[v] = 1;              if(link[v] == -1||DFS(link[v]))              {                  link[v] = u;                  return 1;              }          }      }      return 0;  }  int MaxMatch()  {      int u,res = 0;      memset(link,-1,sizeof(link));      for (u = 1; u <= n; u ++)      {          memset (vis,0,sizeof(vis));          if (DFS(u)) res ++;      }      return res;  }  int main ()  {  #ifdef LOCAL      freopen("in.txt","r",stdin);  #endif      int i,j,x,y;      int cnt = 0;      while (scanf ("%d",&n)&&n)      {          memset (mat,0,sizeof(mat));          for (i = 1; i <= n; i ++)              scanf ("%d%d%d%d",&slide[i].x1,&slide[i].x2,&slide[i].y1,&slide[i].y2);          for (i = 1; i <= n; i ++)          {              scanf ("%d%d",&x,&y);              for (j = 1; j <= n; j ++)                  if (x>slide[j].x1&&x<slide[j].x2&&y>slide[j].y1&&y<slide[j].y2)                      mat[i][j] = 1;          }          printf ("Heap %d\n",++cnt);          int Maxn = MaxMatch();          bool flag = false ;          for (j = 1;j <= n;j ++)              for (i = 1;i <= n;i ++)              {                  if (!mat[i][j]) continue;                  mat[i][j] = 0;                  if (MaxMatch() < Maxn){                      flag = true;                      printf ("(%c,%d) ",'A'+j-1,i);                  }                  mat[i][j] = 1;              }          if (!flag) printf ("none");          printf ("\n\n");      }      return 0;  }  

 


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.