您的位置:首页 > 其它

Poj 1486 Sorting Slides【二分匹配】

2016-10-14 13:07 756 查看
Sorting Slides

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4203 Accepted: 1632
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

Southwestern European Regional Contest 1998

题目大意:

一共给你N个幻灯片,其中给你每个幻灯片的四个坐标:minx,maxx,miny,maxy,然后告诉你每个页码对应标记的位子。

问不能打乱幻灯片的情况下,哪些页码是能够确定下来的。

思路:

1、很明显的二部图,将页码作为左集合,将幻灯片作为右集合,然后我们将页码能够落在的幻灯片和这个页码节点建立一条边,然后跑一遍二分图最大匹配匈牙利算法,将最大匹配数记做tmp;

2、那么我们将得到一个match【i】,然后我们每一次拆除一条匹配边,然后再跑一遍二分图最大匹配匈牙利算法,此时最大匹配数记做tmp2,如果tmp2<tmp,那么说明这个匹配是唯一的,也表示是无法替换的,那么相当于,此幻灯片只能用这个页码来标记,如果不用这个页码来标记,就没有能够将其标记起来的页码了,那么这个页码对应到这个幻灯片,就是能够确定下来的页码。

3、注意输出格式即可。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
int minx,maxx,miny,maxy;
}a[1511];
int n,nota,notb;
int dian [1551][2];
vector<int >mp[1555];
int tmpmatch[1511];
int match[1515];
int vis[1515];
int find(int u)
{
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(nota==v&¬b==u)
{
continue;
}
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int Slove()
{
int ans=0;
memset(match,-1,sizeof(match));
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))ans++;
}
return ans;
}
int main()
{
int kase=0;
while(~scanf("%d",&n))
{
if(n==0)break;
for(int i=0;i<n;i++)
{
mp[i].clear();
}
for(int i=0;i<n;i++)
{
scanf("%d%d%d%d",&a[i].minx,&a[i].maxx,&a[i].miny,&a[i].maxy);
}
for(int i=0;i<n;i++)
{
scanf("%d%d",&dian[i][0],&dian[i][1]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(dian[i][0]<=a[j].maxx&&dian[i][0]>=a[j].minx)
{
if(dian[i][1]<=a[j].maxy&&dian[i][1]>=a[j].miny)
{
mp[i].push_back(j);
}
}
}
}
nota=-1,notb=-1;
int tmp=Slove();
for(int i=0;i<n;i++)tmpmatch[i]=match[i];
printf("Heap %d\n",++kase);
int f=0;
for(int i=0;i<n;i++)
{
int v=tmpmatch[i];
nota=i,notb=v;
int tmp2=Slove();
if(tmp2<tmp)
{
if(f==0)
printf("(%c,%d)",i+'A',v+1);
else
printf(" (%c,%d)",i+'A',v+1);
f++;
}
}
if(f==0)printf("none");
printf("\n\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Poj 1486 pku 1486