您的位置:首页 > 其它

POJ 1486 Sorting Slides 最大二分匹配 匈牙利算法

2015-10-15 09:25 549 查看
Sorting Slides

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


先做一边最大二分匹配。题目问的是这个匹配是否是确定的。那么只要去掉他们匹配的线再做一次,如果最大二分匹配还是和原先的最大二分匹配一样那说明这个匹配是可变的。不是唯一的。。如果不相等的话。说明匹配是唯一的

#include<stdio.h>
#include<string.h>

struct orzcfx{
int x1;
int x2;
int y1;
int y2;
};
struct orzpoint{
int x;
int y;
};

int n,m;
int cc[1111];
bool imap[1111][1111],d[1111];
int s[1111];
bool findOrz(int k){
int i;
for(i=0;i<n;i++){
if(!d[i]&&imap[k][i]){
d[i]=1;
if(s[i]==-1||findOrz(s[i])){
s[i]=k;
return true;
}
}
}
return false;
}

int main(){
orzcfx cfx[1111];
int i,j,k,l,t1,t2,t,orz;
int cas=1;
int x,y;
while(~scanf("%d",&n)&&n){
memset(imap,0,sizeof(imap));
memset(cc,0,sizeof(cc));
memset(s,-1,sizeof(s));
for(i=0;i<n;i++){
scanf("%d%d%d%d",&cfx[i].x1,&cfx[i].x2,&cfx[i].y1,&cfx[i].y2);
}
for(i=0;i<n;i++){
scanf("%d%d",&x,&y);
for(j=0;j<n;j++){ //判断序号是否在里面
if(x>cfx[j].x1 &&x<cfx[j].x2 &&y>cfx[j].y1 &&y<cfx[j].y2){
imap[i][j]=1;
}
}
}
int ans=0;
int orzjud=0;
int flag=0;
for(i=0;i<n;i++){
memset(d,0,sizeof(d));
if(findOrz(i)){
ans++;
}
}
for(i=0;i<n;i++){
cc[i]=s[i];
//	printf("%d  ",s[i]);
}
printf("Heap %d\n", cas++);
/*
for(int ii=0;ii<n;ii++){
for(int jj=0;jj<n;jj++){
printf("%d " ,imap[ii][jj]);
}
printf("\n");
}*/
if(ans==n)
{
for(i=0;i<n;i++)
{
memset(s,-1,sizeof(s));
imap[cc[i]][i]=0; //删边
/*
for(int ii=0;ii<n;ii++){
for(int jj=0;jj<n;jj++){
printf("%d " ,imap[ii][jj]);
}
printf("\n");
}*/
int orzfind= 0;
for(j=0;j<n;j++){
memset(d,0,sizeof(d));
if(findOrz(j)) orzfind++;
}
if(orzfind==n)  continue;
else
{
if(flag)
printf(" ");
printf("(%c,%d)",'A'+i,cc[i]+1);
flag=1;
}
imap[cc[i]][i]=1;
}

}
if(!flag)
printf("none");
printf("\n\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息