【POJ】1486:Sorting Slides【二分图关键边判定】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5390 | Accepted: 2095 |
Description
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
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
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
Solution
题意就是找所有可以唯一对应起来的矩形和数字。
看起来这种对应关系很像二分图匹配??
建成二分图后就变成判定哪些边是必须的了。必须的意思是删掉这条边最大匹配数会变小,所以对所有边删边后做一次最大匹配,与最初始的最大匹配数进行比较即可。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; int n, vis[], to[], pi[];
int xi[], xa[], yi[], ya[]; bool check(int x, int y, int i) {
if(x >= xi[i] && x <= xa[i] && y >= yi[i] && y <= ya[i]) return ;
return ;
} int G[][]; bool dfs(int u) {
for(int v = ; v <= n; v ++) {
if(G[u][v]) {
if(!vis[v]) {
vis[v] = ;
if(!pi[v] || dfs(pi[v])) {
pi[v] = u;
to[u] = v;
return ;
}
}
}
}
return ;
} int match() {
memset(to, , sizeof(to));
memset(pi, , sizeof(pi));
int ans = ;
for(int i = ; i <= n; i ++) {
if(!to[i]) {
memset(vis, , sizeof(vis));
ans += dfs(i);
}
}
return ans;
} int main() {
int ti = ;
while(~scanf("%d", &n)) {
if(n == ) break;
memset(G, , sizeof(G));
for(int i = ; i <= n; i ++)
scanf("%d%d%d%d", &xi[i], &xa[i], &yi[i], &ya[i]);
for(int i = ; i <= n; i ++) {
int x, y;
scanf("%d%d", &x, &y);
for(int j = ; j <= n; j ++) {
if(!check(x, y, j)) continue;
G[j][i] = ;
}
}
int ma = match(); int flag = ;
printf("Heap %d\n", ++ ti);
for(int i = ; i <= n; i ++) {
for(int j = ; j <= n; j ++)
if(G[i][j]) {
G[i][j] = ;
if(match() < ma) {
printf("(%c,%d) ", i + , j);
flag = ;
}
G[i][j] = ;
}
}
if(!flag) printf("none\n\n");
else printf("\n\n");
}
return ;
}
最新文章
- myql 查询树形表结果:说说、说说的述评、评论的回复
- R获取股票数据
- sed 例子
- 巧架个人BT服务器
- ios入门之c语言篇——基本函数——1——随机数生成
- svn创建并应用补丁
- VPS(Virtual Private Server 虚拟专用服务器)[转自百度]
- react-dom.js 源码
- Maven项目中,系统设置的CLASSPATH环境变量问题
- C# 多线程学习笔记 - 3
- UML作业第三次:分析《书店图书销售管理系统》,绘制类图
- Javascript高级编程学习笔记(68)—— 事件(12)设备事件
- 基于SSM框架贺州学院校园二手交易平台设计与实现
- linux学习笔记-目录结构(2)
- DrawFrameControl 绘制标准控件
- [转] 在图标库中,找到合适的图标 ico
- Privoxy
- SAC学习笔记(一)——SAC安装
- CKEditor4.x整合教程-Xproer.WordPaster
- C#基础 一(方法详解)
热门文章
- 当遇到not a dynamic executable时怎么做
- C 数据结构堆
- Python和MySQL数据库交互PyMySQL
- 十五、springboot集成定时任务(Scheduling Tasks)(二)之(线程配置)
- spring boot 中使用redis session
- 打开mvc项目无法运行,报";Unable to launch the IIS Express Web server";
- java基础50 配置文件类(Properties)
- mysql数据库主从同步复制原理
- spring-boot分环境打包为tar包
- Java学习(Map接口)