POJ 1486

  本来以为水题一笔带过……结果调了大半个小时……

Sorting Slides
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1901 Accepted: 688

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

【Analysis】

  题目求二分图的必须边,即在二分图G中,i∈G只能与j∈G匹配。

  具体算法也是个条块的东西……

  先求一遍最大匹配,把匹配的结果处理出来,然后枚举每个匹配边,删边,以匹配边得一端增广,若找不到增广路则此为必须边。

  

program POJ_1486;
type pos=record
          x1,x2,y1,y2:longint;
     end;
     pos2=record
               x,y:longint;
     end;

var point:Array[1..1000]of pos;
    num:array[1..1000]of pos2;
    match:array[1..1000]of longint;
    vis:array[1..1000]of boolean;
    n,cases,i,j,u:longint;
    flag:boolean;
    map:array[1..1000,1..1000]of 0..1;

function dfs(u:longint):boolean;
var i:longint;
begin
  for i:=1 to n do
    if (not vis[i])and(map[u,i]>0) then
      begin
        vis[i]:=true;
        if (match[i]=-1)or(dfs(match[i])) then
          begin
            match[i]:=u;
            exit(true);
          end;
      end;
  exit(false);
end;

begin
  Cases:=1;
  readln(n);
  while n<>0 do
    begin
      fillchar(match,sizeof(match),$ff);
      fillchar(map,sizeof(map),0);
      writeln('Heap ',cases);
      inc(cases);
      for i:=1 to n do
        with point[i] do
          readln(x1,x2,y1,y2);
      for i:=1 to n do
        with num[i] do
          readln(x,y);
     for i:=1 to n do
       for j:=1 to n do
         with point[i] do
           if (num[j].x>=x1)and(num[j].x<=x2)and(num[j].y>=y1)and(num[j].y<=y2) then
             map[j,i]:=1;
     for i:=1 to n do
       begin
         fillchar(vis,sizeof(vis),false);
         dfs(i);
       end;
     flag:=false;
     for i:=1 to n do
       begin
         u:=match[i];
         if u=-1 then continue;
         match[i]:=-1;
         map[u,i]:=0;
         fillchar(vis,sizeof(vis),false);
         if not dfs(u) then
           begin
             match[i]:=u;
             if flag then write(' ');
             flag:=true;
             write('(',chr(ord('A')+i-1),',',match[i],')');
           end;
         map[u,i]:=1;
       end;
     if not flag then write('none');;
     writeln;writeln;
     readln(n);
    end;
  readln;readln;
end.

  这个题其实不难……但是由于题目没给数据范围,POJ还猥琐地卡10000K内存,我就M了……修正之后又出现了罕见的Presentation error……原来是少输出了一个回车……囧……看来我真的是半秃……

原文地址:https://www.cnblogs.com/Delostik/p/1989263.html