Sorting Slides
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2259 Accepted: 828
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
代码二:
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2259 Accepted: 828
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
二分图最大匹配问题,,,,但是让输出的是必须边,,,就是如果i属于G,j属于G当且仅当i和j匹配时为必须边,
这里用到的主要是先求出最大匹配后,再求进行匹配一轮,如果某个点不能在被匹配,则输出,否者不输出。
代码:
#include<iostream>
#include<string.h>
#define N 30
using namespace std;
struct point{ int minx,maxx,miny,maxy;
}aa[N];
struct Node{ int x,y;
}bb[N];
int n;
int visit[N];
int match[N];
int map[N][N];
bool check(int &j,int &i)
{ return (bb[j].x>=aa[i].minx&&bb[j].x<=aa[i].maxx&&bb[j].y>=aa[i].miny&&bb[j].y<=aa[i].maxy);}
void read()
{ memset(map,0,sizeof(map));
for(int i=1;i<=n;i++)
cin>>aa[i].minx>>aa[i].maxx>>aa[i].miny>>aa[i].maxy;
for(int i=1;i<=n;i++)
cin>>bb[i].x>>bb[i].y;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(check(i,j))
map[i][j]=1;
}
bool Search(int a)
{ for(int i=1;i<=n;i++)
if(visit[i]&&map[a][i])
{ visit[i]=false;
if(match[i]==0||Search(match[i]))
{ match[i]=a;
return true;
}
} return false;
}
void _match()
{ memset(match,0,sizeof(match));
for(int i=1;i<=n;i++)
{ memset(visit,true,sizeof(visit));
Search(i);
}
int ok=0;
for(int i=1;i<=n;i++)
{ int u=match[i];
if(u==0) continue;
match[i]=0;
memset(visit,true,sizeof(visit));
map[u][i]=0;
if(!Search(u))
{ ok++;
match[i]=u;
if(ok>1) cout<<" ";
printf("(%c,%d)",i+'A'-1,match[i]);
}
map[u][i]=1;
}
if(!ok) cout<<"none";
cout<<endl<<endl;
}
int main()
{ int tot=0;
while(cin>>n&&n)
{ read() ;
cout<<"Heap "<< ++tot<<endl;
_match();
} return 0;
}
代码二:
/*
幻灯片排序,有 N 个幻灯片,顺序乱了,每个幻灯片上写有 1 个数字,表示这个幻灯片的顺序。
现在不知道每张幻灯片上写的是哪个数字,但是可能可以推导出哪个数字写在哪个幻灯片上:
给出幻灯片的 4 个顶点坐标和从 1 到 N 的写在幻灯片上那个数字的坐标,求哪个幻灯片和哪个数字相对应。
直接二分最大匹配。。。。。。一边是幻灯片,一边是数字,哪个和哪个可能匹配则连一条边。
*/
#include <iostream>
#include<cstdio>
using namespace std;
int n;
struct Point
{
int minx,maxx,miny,maxy;
}p[30];
struct NODE
{
int x,y;
}nd[30];
int map[30][30];
int mat[30],use[30];
bool check(int i,int j)
{
return (nd[i].x>=p[j].minx && nd[i].x<=p[j].maxx && nd[i].y>=p[j].miny && nd[i].y<=p[j].maxy);
}
void read()
{
int i,j;
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
scanf("%d%d%d%d",&p[i].minx,&p[i].maxx,&p[i].miny,&p[i].maxy);
for(i=1;i<=n;i++)
scanf("%d%d",&nd[i].x,&nd[i].y);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(check(i,j))
{
map[i][j]=1;
}
}
}
}
bool path(int u)
{
for(int i=1;i<=n;i++)
if(map[u][i]&&!use[i])
{
use[i]=1;
if(mat[i]==0||path(mat[i]))
{
mat[i]=u;
return 1;
}
}
return 0;
}
void Maxmatch()
{
memset(mat,0,sizeof(mat));
for(int i=1;i<=n;i++)
{
memset(use,0,sizeof(use));
path(i);
}
int ok=0;
for(int i=1;i<=n;i++)
{
int u=mat[i];
if(u==0)continue;
mat[i]=0;
memset(use,0,sizeof(use));
map[u][i]=0;
if(!path(u))
{
ok++;
mat[i]=u;
if(ok>1)
printf(" ");
printf("(%c,%d)",i+64,mat[i]);
}
map[u][i]=1;
}
if(!ok)
printf("none");
printf("\n\n");
}
int main()
{
int test=0;
while(~scanf("%d",&n)!=EOF&&n)
{
read();
printf("Heap %d\n",++test);
Maxmatch();
}
}