ZOJ——1197 Sorting Slides(二分匹配)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1197

题目:

Sorting Slides

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 s 
{
	int a,b,c,d;
}aa[110];
int a,b,sum;
int aaa[110],bo[110];
int e[110][110];
int dfs(int x)
{
	int i;
	for( i = 1;i <= a;i ++){
		if(!bo[i] && e[x][i]){
			bo[i] = 1;
			if(aaa[i] == -1 || dfs(aaa[i])){
				aaa[i] = x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int i,j,t = 1;
	while(scanf("%d",&a),a != 0){
		memset(e,0,sizeof(e));
		for(i = 1;i <= a;i ++){
			scanf("%d%d%d%d",&aa[i].a,&aa[i].b,&aa[i].c,&aa[i].d);
		}
		for(i = 1;i <= a;i ++ ){
			int x,y;
			scanf("%d%d",&x,&y);
			for(j = 1;j <= a;j ++){
				 if(aa[j].a <= x && x <= aa[j].b && aa[j].c <= y && y <= aa[j].d )  
                    e[j][i] = 1;  
			}
		}
		memset(aaa,-1,sizeof(aaa));
		sum = 0;
		for(i= 1;i <= a;i ++){
			memset(bo,0,sizeof(bo));  
			if(dfs(i))
				sum ++;
		}
		int flag =0;
		printf("Heap %d\n",t++);
		for(i = 1;i <= a;i ++){
			for(j = 1;j <= a ;j ++){
				if(e[i][j]){
					e[i][j] = 0;
					int kk = 0,k;
					memset(aaa,-1,sizeof(aaa));
					for(k= 1;k <= a;k ++){
						memset(bo,0,sizeof(bo));  
						if(dfs(k))
							kk ++;
					}
					if(sum != kk){
						if(flag)
							putchar(' ');
						printf("(%c,%d)",i+'A'-1,j);
						flag++;
					}
					
					e[i][j] = 1;
				}
			}
		}
		if(!flag)
			printf("none\n\n");
		else
			printf("\n\n");
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/hunt_er/article/details/79304774