poj1486 G - Sorting Slides【二分匹配唯一边】【第三周练习】【the fifth day】

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

题意:读入n个矩形的坐标范围和每个坐标点的坐标,问怎样匹配才能使每个点唯一的包括在矩形内部。

思路:实现二分最大匹配,枚举删除已匹配边,如果删除该匹配边后无法实现最大匹配,说明该条边对应的两个点唯一匹配,输出该边。

总结:自己的思路只对了最大二分匹配这一部分,后面的判断唯一边根本没有想到,虽然用了很多时间去证明自己的错误思路,但是这种情况下再去看题解真的感觉收获挺大滴~~

#include<stdio.h>
#include<string.h>
const int N = 100+10;
int n;
int path[N],book[N],e[N][N]; 
int vis[N];
struct node{
	int xmin,xmax,ymin,ymax;
}num[N];

struct Node{
	int x,y;
}num2[N];
struct rnode{
	char s;
	int e;
}num3[N];
int dfs(int u)//匈牙利算法 
{
	for(int i = 1; i <= n; i ++)
	{
		if(!book[i]&&e[u][i])
		{
			book[i] = 1;
			if(path[i] == -1||dfs(path[i])!=0)
			{
				path[i] = u; 
				return 1;
			}
		}
	}
	return 0;
}

int find()
{
	int ans = 0;
	memset(path,-1,sizeof(path));
	for(int i = 1; i <= n; i ++)
	{
		memset(book,0,sizeof(book));
		if(dfs(i))
			ans++;
	}
	return ans;
}

int main()
{
	int t = 0,flag,sum,ans;
	while(scanf("%d",&n),n!=0)
	{
		
		memset(e,0,sizeof(e));
		for(int i = 1; i <= n; i ++)
			scanf("%d%d%d%d",&num[i].xmin ,&num[i].xmax ,&num[i].ymin ,&num[i].ymax );
		for(int i = 1; i <= n; i ++)//对矩形和点分别进行编号 
		{
			scanf("%d%d",&num2[i].x,&num2[i].y);
			for(int j = 1; j <= n; j ++)
			//如果该坐标在矩形内,则相匹配 
				if(num2[i].x >= num[j].xmin &&num2[i].x<= num[j].xmax &&num2[i].y >= num[j].ymin&&num2[i].y <= num[j].ymax)
					e[i][j] = 1;
		}
		flag = 0;
		printf("Heap %d\n",++t);
		ans = find();
		if(ans != n)//如果不满足最大匹配 
		{
			printf("none\n\n");
			continue;
		}
		flag = 1;
		for(int i = 1; i <= n;i ++)
			vis[i] = path[i];
		sum = 0;
		for(int i = 1; i <= n; i ++)
		{
			e[vis[i]][i] = 0;
			if(ans != find())//如果去掉该边的两个点无法完成最大匹配,说明是必须边 
			{
				num3[sum].s =  i-1+'A';
				num3[sum].e = vis[i];
				sum ++;
				flag = 0;
			}	
			e[vis[i]][i] = 1;//记得恢复
		}
		if(flag)
			printf("none");
		else
		{
			for(int i = 0; i < sum-1; i ++)
				printf("(%c,%d) ",num3[i].s  ,num3[i].e );//orz,这种低级错误也能犯 
			printf("(%c,%d)",num3[sum-1].s  ,num3[sum-1].e );
		}
		printf("\n\n");//每个样例后有空行。。。 
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值