【POJ - 1486】Sorting Slides(思维建图,二分图求必须边,关建边,图论)

题干:

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

题目大意:

给出一些重叠的矩形,按照给出的顺序分别编号为A、B、C,对于每个矩形给出左下角和右上角的xy坐标(注意读入顺序)。然后再给出一些点,这些点画在了某个矩形上,根据给出的顺序分别编号为1,2,3,对于每个点给出其xy坐标。矩形是透明的所以一次就可以看到所有的点和所有矩形的外轮廓,问能否确定某个点一定在某个矩形上(每个矩形上都有且只有一个点)。

如果可以确定一对编号,则以成对形式输出确定对应的纸编号和数字(也就是说不一定输出n对)。如果一个确定的对应都没有,则输出none。

解题报告:

不难看出是个二分图模型,首先判断是否可以完全匹配,在此基础上看能否一一对应。也就是说满足none的情况有两种,要么是匹配数都不是n,要么是一个必须边都没有。

当然这题这么做复杂度还有有点高,在判断必须边的时候是n^2枚举的然后再跑匈牙利,复杂度就有点高,其实可以换种思考方式,如果他是必须边,那就只可能是你第一次求出的nxt数组中匹配到的值,所以我们可以建边的时候不是f[j][i]=1,而是f[i][j]=1,然后求出一组nxt赋值给xM数组,这样枚举第二维,直接调用xM[i]就可以求出对应的左侧的二分图中的值(第一维),那么直接操作的边就是f[xM[i]][i],如果去掉这个边之后match()变了,那么这一定就是必须边了,直接输出 i+'A'-1 , xM[i]就行,因为你是枚举的第二维,而建边的时候正好就是第二维是纸张(用ABCD表示的那个而非1234表示的那个)而题目要求正好是优先字母的字典序升序,所以正好可以堆起来了。(相应的代码是AC代码2)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 555 + 5;
bool used[MAX];
int nxt[MAX],n,f[MAX][MAX];
bool find(int x) {for(int i = 1; i<=n; i++) {if(used[i] == 0 && f[x][i]) {used[i] = 1;if(nxt[i] == 0|| find(nxt[i])) {nxt[i] = x;return 1;} }}return 0 ;
}
int match() {memset(nxt,0,sizeof nxt);int res = 0;for(int i = 1; i<=n; i++) {memset(used,0,sizeof used);if(find(i)) res++;}return res;
}
struct Node {int x1,x2,y1,y2;
} nn[MAX];
int x[MAX],y[MAX];
int main()
{int iCase = 0;while(~scanf("%d",&n) && n) {memset(f,0,sizeof f);memset(nxt,0,sizeof nxt);if(iCase!=0) puts("");printf("Heap %d\n",++iCase);for(int i = 1; i<=n; i++) {scanf("%d%d%d%d",&nn[i].x1,&nn[i].x2,&nn[i].y1,&nn[i].y2);}for(int i = 1; i<=n; i++) {scanf("%d%d",x+i,y+i);for(int j = 1; j<=n; j++) {if(x[i]>=nn[j].x1&&x[i]<=nn[j].x2&&y[i]>=nn[j].y1&&y[i]<=nn[j].y2) f[j][i] = 1;}}int ans = match(),cnt = 0; if(ans != n) {puts("none");continue;	}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(f[i][j] == 0) continue;f[i][j] = 0;if(match() != n) {if(cnt) printf(" "); printf("(%c,%d)",i+'A'-1,j);cnt++;}f[i][j] = 1;}}if(cnt == 0) puts("none"); else puts("");}return 0 ;
}

AC代码2:链接

#include<stdio.h>
#include<string.h>
#include<iostream>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int> P;
const int MAXN = 30;
int uN, vN;
int g[MAXN][MAXN], linker[MAXN], tmp[MAXN];
bool used[MAXN];
bool dfs(int u)
{for(int v = 0; v < vN; v++)if(g[u][v] && !used[v]){used[v] = 1;if(linker[v] == -1 || dfs(linker[v])){linker[v] = u; return 1;}}return 0;
}
int hungary()
{int res = 0;memset(linker, -1, sizeof(linker));for(int u = 0; u < uN; u++){memset(used, 0, sizeof used);if(dfs(u)) res++;}return res;
}
struct node{int x1, x2, y1, y2;
}p[30];
bool inside(P &u, node &v)
{if(u.first > v.x1 && u.first < v.x2 && u.second > v.y1 && u.second < v.y2) return true;return false;
}
int main()
{int Heap = 1;while(scanf("%d", &uN), uN){memset(g, 0, sizeof g);vN = uN;for(int i = 0; i < uN; i++)scanf("%d %d %d %d", &p[i].x1, &p[i].x2, &p[i].y1, &p[i].y2);P t;for(int i = 0; i < uN; i++){scanf("%d %d", &t.first, &t.second);for(int j = 0; j < uN; j++)if(inside(t, p[j]))g[i][j] = 1;}printf("Heap %d\n", Heap++);int ans = hungary();if(ans != vN){puts("none\n");continue;}bool flag = 0;memcpy(tmp, linker, sizeof(tmp));for(int i = 0; i < vN; i++){g[tmp[i]][i] = 0;if(ans != hungary()){printf("(%c,%d) ", 'A' + i, tmp[i] + 1);flag = 1;}g[tmp[i]][i] = 1;}if(!flag) puts("none\n");elseputs("\n");}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440336.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用OpenSSL编写SSL,TLS程序

http://zhoulifa.bokee.com/6134045.html http://blog.sina.com.cn/s/blog_86ca13bb0100vaph.html http://blog.chinaunix.net/uid-26575352-id-3048856.html 一、简介: SSL(SecureSocket Layer)是netscape公司提出的主要用于web的安全通信标准,分为2.0版和3.0版.TLS(Transport…

信息技术计算机伦理与安全教案,龙教版信息技术七年级下册第7课 安全与道德 教案...

ID:9954219分类&#xff1a;全国,2019资源大小&#xff1a;228KB资料简介:题 目第七课 安全与道德总课时1设计来源自我设计教学时间教材分析这节课计算机与网络安全部分定义介绍和叙述较多,所以为了避免枯燥可以设计课件和并准备病毒计算机安全报道的视频、多媒体讲解、图片等…

【HDU - 5706】GirlCat(bfs)

题干&#xff1a; As a cute girl, Kotori likes playing Hide and Seek with cats particularly. Under the influence of Kotori, many girls and cats are playing Hide and Seek together. Koroti shots a photo. The size of this photo is nmnm, each pixel of the ph…

8.Using Categorical Data with One Hot Encoding

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将了解“分类”变量是什么&#xff0c;以及处理此类数据的最常用方法。 Introduction 分类数据是仅采用有限数量值的数据。 例如&#xff0c;如果人们回答一项关于他们拥有哪种品牌汽车的调查&#xff0c;结果将是明…

iPhone换屏幕测试软件,怎样检验iPhone是否更换过屏幕?

原标题&#xff1a;怎样检验iPhone是否更换过屏幕&#xff1f;关注下图公众号&#xff0c;鉴定苹果手机真假↓↓↓购买新手机时&#xff0c;到手后会想手机各零部件是否是正品原装&#xff0c;就好比屏幕是否原装屏&#xff01;入手一部iPhone新机的时候&#xff0c;该如何检验…

*【HDU - 5707】Combine String(dp)

题干&#xff1a; Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb. A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read the…

《TCP/IP详解》学习笔记(一):基本概念

为什么会有TCP/IP协议 在世界上各地&#xff0c;各种各样的电脑运行着各自不同的操作系统为大家服务&#xff0c;这些电脑在表达同一种信息的时候所使用的方法是千差万别。就好像圣经中上帝打乱 了各地人的口音&#xff0c;让他们无法合作一样。计算机使用者意识到&#xff0c;…

【POJ - 3272】Cow Traffic(dp,建反向图,DAG拓扑图)

题干&#xff1a; The bovine population boom down on the farm has caused serious congestion on the cow trails leading to the barn. Farmer John has decided to conduct a study to find the bottlenecks in order to relieve the traffic jams at milking time. The…

pc服务器不同型号,服务器与PC系统软件之不同

服务器与PC系统软件之不同对于中关村在线的网友来说&#xff0c;PC系统应该都不陌生&#xff0c;而且分分钟重装的水准。但在笔者过往的服务器装机经验中&#xff0c;可谓是一部千年血泪史。服务器和PC系统差别还是很大的。现在的PC系统多是windows7和windows10&#xff0c;而在…

9.XGBoost

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将学习如何使用功能强大的xgboost库构建和优化模型。 What is XGBoost XGBoost是处理标准表格数据的领先模型&#xff08;您在Pandas DataFrames中存储的数据类型&#xff0c;而不是像图像和视频这样的更奇特的数据类…

*【HDU - 5711】Ingress(tsp旅行商问题,优先队列贪心,状压dp,floyd最短路,图论)

题干&#xff1a; Brickgao, who profited from your accurate calculating last year, made a great deal of money by moving bricks. Now he became gay shy fool again and recently he bought an iphone and was deeply addicted into a cellphone game called Ingress. …

ajax get请求成功,成功()函数的AJAX GET请求

后不叫我有一个jQuery的AJAX脚本像下面&#xff1a;成功()函数的AJAX GET请求function FillCity() {var stateID $("#ddlState").val();$.ajax({url: Url.Action("Employee", "Index"),type: "GET",dataType: "json",data:…

《TCP/IP详解》学习笔记(二):数据链路层

数据链路层有三个目的&#xff1a; 为IP模块发送和 接收IP数据报。为ARP模块发送ARP请求和接收ARP应答。为RARP发送RARP请 求和接收RARP应答ip大家都听说过。至于ARP和RARP&#xff0c;ARP叫做地址解析协议&#xff0c;是用IP地址换MAC地址的一种协议&#xff0c;而RARP则叫…

【POJ - 2762】Going from u to v or from v to u?(Tarjan缩点,树形dp 或 拓扑排序,欧拉图相关)

题干&#xff1a; In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the o…

《TCP/IP详解》学习笔记(三):IP协议、ARP协议

把这三个协议放到一起学习是因为这三个协议处于同一层,ARP 协议用来找到目标主机的 Ethernet 网卡 Mac 地址,IP 则承载要发 送的消息。数据链路层可以从 ARP 得到数据的传送信息,而从 IP 得到要传输的数据信息。 IP 协议 IP 协议是 TCP/IP 协议的核心,所有的 TCP,UDP,IMCP,IGCP…

光与夜之恋服务器维护中,光与夜之恋7月16日停服维护说明 维护详情一览

光与夜之恋7月16日停服维护说明维护详情一览。光与夜之恋7月16日停服维护更新了哪些内容?我们去了解一下。【7月16日停服维护说明】亲爱的设计师&#xff1a;为了给设计师们提供更好的游戏体验&#xff0c;光启市将于7月16日(周五)00:00进行预计5小时的停服维护&#xff0c;可…

10.Partial Dependence Plots

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将学习如何创建和解释部分依赖图&#xff0c;这是从模型中提取洞察力的最有价值的方法之一。 What Are Partial Dependence Plots 有人抱怨机器学习模型是黑盒子。这些人会争辩说我们无法看到这些模型如何处理任何给定的数据…

springboot监控服务器信息,面试官:聊一聊SpringBoot服务监控机制

目录前言任何一个服务如果没有监控&#xff0c;那就是两眼一抹黑&#xff0c;无法知道当前服务的运行情况&#xff0c;也就无法对可能出现的异常状况进行很好的处理&#xff0c;所以对任意一个服务来说&#xff0c;监控都是必不可少的。就目前而言&#xff0c;大部分微服务应用…

0.《Apollo自动驾驶工程师技能图谱》

【新年礼物】开工第一天&#xff0c;送你一份自动驾驶工程师技能图谱&#xff01; 布道团队 Apollo开发者社区 1月 2日 AI时代到来&#xff0c;人才的缺乏是阻碍行业大步发展的主要因素之一。Apollo平台发布以来&#xff0c;我们接触到非常多的开发者他们并不是专业自动驾驶领…

【HDU - 1116】【POJ - 1386】Play on Words(判断半欧拉图,欧拉通路)

题干&#xff1a; Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large number…