zoukankan      html  css  js  c++  java
  • poj 1486 Sorting Slides(二分图匹配的查找应用)

    Description
    Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minimalist, he wants to do this with the minimum amount of work possible. 
    
    The situation is like this. The slides all have numbers written on them according to their order in the talk. Since the slides lie on each other and are transparent, one cannot see on which slide each number is written. 


    Well, one cannot see on which slide a number is written, but one may deduce which numbers are written on which slides. If we label the slides which characters A, B, C, ... as in the figure above, it is obvious that D has number 3, B has number 1, C number 2 and A number 4. 
    
    Your task, should you choose to accept it, is to write a program that automates this process.

    Input

    The input consists of several heap descriptions. Each heap descriptions starts with a line containing a single integer n, the number of slides in the heap. The following n lines contain four integers xmin, xmax, ymin and ymax, each, the bounding coordinates of the slides. The slides will be labeled as A, B, C, ... in the order of the input. 
    
    This is followed by n lines containing two integers each, the x- and y-coordinates of the n numbers printed on the slides. The first coordinate pair will be for number 1, the next pair for 2, etc. No number will lie on a slide boundary. 
    
    The input is terminated by a heap description starting with n = 0, which should not be processed. 

    Output

    For each heap description in the input first output its number. Then print a series of all the slides whose numbers can be uniquely determined from the input. Order the pairs by their letter identifier. 
    
    If no matchings can be determined from the input, just print the word none on a line by itself. 
    
    Output a blank line after each test case. 

    Sample Input

    4
    6 22 10 20
    4 18 6 16
    8 20 2 18
    10 24 4 8
    9 15
    19 17
    11 7
    21 11
    2
    0 2 0 2
    0 2 0 2
    1 1
    1 1
    0

    Sample Output

    Heap 1
    (A,4) (B,1) (C,2) (D,3)
    
    Heap 2
    none

    Source

     
    给出一些矩形的坐标和一些点的坐标,若点在矩形内,则该点和该矩形匹配,问是否存在某个匹配在所有的完美匹配中,这题可以先任意找出一个完美匹配,然后依次删除该匹配的每一条边,若仍能构成完美匹配,则这个匹配不唯一,若不能构成完美匹配,则该匹配唯一
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 #define N 100
     6 int n;
     7 int x1[N],x2[N],y1[N],y2[N];
     8 int mp[N][N];
     9 int match[N];
    10 int path[N];
    11 int vis[N];
    12 bool dfs(int x){
    13     for(int i=0;i<n;i++){
    14         if(!vis[i] && mp[x][i]){
    15             vis[i]=1;
    16             if(match[i]==-1 || dfs(match[i])){
    17                 match[i]=x;
    18                 return true;
    19             }
    20         }
    21     }
    22     return false;
    23 }
    24 int solve(){
    25     int ans=0;
    26     memset(match,-1,sizeof(match));
    27 
    28     for(int i=0;i<n;i++){
    29         memset(vis,0,sizeof(vis));
    30         if(dfs(i)){
    31             ans++;
    32         }
    33     }
    34     return ans;
    35 }
    36 int main()
    37 {
    38     int ac=0;
    39     while(scanf("%d",&n)==1 && n){
    40             printf("Heap %d
    ",++ac);
    41         for(int i=0;i<n;i++){
    42             scanf("%d%d%d%d",&x1[i],&x2[i],&y1[i],&y2[i]);
    43         }
    44         memset(mp,0,sizeof(mp));
    45         for(int i=0;i<n;i++){
    46             int a,b;
    47             scanf("%d%d",&a,&b);
    48             for(int j=0;j<n;j++){
    49                 if(a>x1[j] && a<x2[j] && b>y1[j] && b<y2[j]){
    50                     mp[i][j]=1;
    51                 }
    52             }
    53         }
    54 
    55         int ans=solve();
    56         //printf("===%d
    ",ans);
    57         int flag=0;
    58         if(ans==n){
    59             for(int i=0;i<n;i++){
    60                 path[i]=match[i];
    61             }
    62             for(int i=0;i<n;i++){
    63                 int u=path[i];
    64                 mp[u][i]=0;//这个表示匹配的每一条边?
    65                 if(solve()==n) continue;
    66                 else{
    67                     if(flag)
    68                       printf(" ");
    69                     printf("(%c,%d)",'A'+i,path[i]+1);
    70                     flag=1;
    71                 }
    72                 mp[u][i]=1;
    73 
    74             }
    75         }
    76         if(!flag){
    77             printf("none");
    78         }
    79         printf("
    
    ");
    80 
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    cancel-ng-swipe-right-on-child
    css.day.05.eg
    css.day05
    css.day04.eg
    css.day04
    css.day03.eg
    css.day03
    css.day02.eg
    九月十月百度人搜,阿里巴巴,腾讯华为笔试面试八十题(第331-410题)(转)
    阿里巴巴笔试题选解
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4773532.html
Copyright © 2011-2022 走看看