点击打开题目链接
Sorting Slides
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 2961 |
|
Accepted: 1140 |
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张幻灯片堆在了一起,这一堆幻灯片上共有N个数字,这些数字与幻灯片是一一对应的。
但是由于幻灯片都是透明的,所以现在无法直接确定哪个数字属于哪张幻灯片。
现在告诉你这N张幻灯片和N个数字的位置,编程输出可以确定的数字和幻灯片之间的对应关系。
=====================================算法分析=====================================
将数字集合作为U,幻灯片集合作为V,数字u可能在幻灯片v上作为连接u和v的边。
一、二分图最大匹配。
根据题意可知,所建立二分图的最大匹配数就是N,且最大匹配方案可能有多种。
而u和v的对应关系确定,也就是说在所有最大匹配方案中,u的匹配点都是v。
那么如果删除连接u和v的边,二分图的最大匹配数一定会小于N,根据该思路编程即可。
二、贪心。
如果当前图中有“某个端点的度为1”的边,则该边所表示的对应关系便是确定的(这一点毋庸置疑),从图中删除该边的两
个端点后继续此操作。
在网上的题解和POJ的[Discuss]中发现有不少人这样做。
但是他们都没有证明确保这一算法正确的前提“所有点的度不小于1的二分图中任意去除一条边都不会使得最大匹配数减少”。
事实上这一点也不难证明,以下是浩神提供的思路。
首先求出二分图的最大匹配。
显然删除一条非匹配边是不可能使得最大匹配数减少的。
那么删除一条匹配边呢?首先这时候图中的匹配数会减一,另外不妨假设所删除匹配边的两个端点分别是u和v。
而由于图中所有点的度是不小于1的,这就意味着u和v一定同时位于某个环中,那么从u到v就不止“直接连接u和v的边”这一条路径。
所以虽然删除了从u到v的匹配边使得u和v变成未盖点,但此时我们又能找到另外一条从u到v的路径,而这条路径显然即增广路。
对增广路进行取反操作后图中的匹配数便会加1,那么前后图中的匹配数也就保持不变,也就是说最大匹配数并未减少。
觉得写起来麻烦,就不贴代码了。
=======================================代码=======================================
#include<stdio.h>
#include<string.h>
int N,Linker[50];
bool Edge[50][50],Vis[50];
struct RECT { int Xmin,Xmax,Ymin,Ymax; } Rect[50];
bool DFS(int U)
{
for(int v=1;v<=N;++v)
{
if(Edge[U][v]&&!Vis[v])
{
Vis[v]=true;
if(Linker[v]==-1||DFS(Linker[v]))
{
Linker[v]=U;
return true;
}
}
}
return false;
}
int Hungary()
{
memset(Linker,-1,sizeof(Linker));
int ans=0;
for(int u=1;u<=N;++u)
{
memset(Vis,0,sizeof(Vis));
if(DFS(u)) { ++ans; }
}
return ans;
}
void ReaData()
{
memset(Edge,0,sizeof(Edge));
for(int v=1;v<=N;++v)
{
scanf("%d%d",&Rect[v].Xmin,&Rect[v].Xmax);
scanf("%d%d",&Rect[v].Ymin,&Rect[v].Ymax);
}
for(int u=1;u<=N;++u)
{
int x,y;
scanf("%d%d",&x,&y);
for(int v=1;v<=N;++v)
{
if(!(Rect[v].Xmin<=x&&x<=Rect[v].Xmax)) { continue; }
if(!(Rect[v].Ymin<=y&&y<=Rect[v].Ymax)) { continue; }
Edge[u][v]=true;
}
}
}
int main()
{
int Case=1;
while(scanf("%d",&N)==1&&N)
{
ReaData();
printf("Heap %d\n",Case++);
bool first=true;
for(int v=1;v<=N;++v)
{
Hungary();
int u=Linker[v];
Edge[u][v]=false;
if(Hungary()!=N)
{
if(first) { first=false; }
else { printf(" "); }
printf("(%c,%d)",v-1+'A',u);
}
Edge[u][v]=true;
}
printf("%s\n",first?"none\n":"\n");
}
return 0;
}