Giáo sư Clumsey sẽ có buổi thuyết trình quan trọng vào chiều nay. Chẳng may thay, ông
là người không mấy gọn gàng và đã để các bản thuyết trình (slides) trong suốt trong một
bao lớn lộn xộn (heap). Trước buổi thuyết trình, ông phải sắp xếp lại các slides này. Ông
muốn các thao tác thực hiện là tối thiểu.
Tất cả các slides được khắc số cho biết thứ tự của chúng. Vì các slides nằm chồng lên
nhau và trong suốt, nên chúng ta không thể xác định được số được khắc trên slide nào.
Chúng ta không thể thấy được một số được khắc trên chính xác một slide nào, nhưng có
thể kết luận được các số thuộc về các slides nào (nếu khó hiểu hãy xem ví dụ và tham
khảo bản gốc). Ví dụ nếu chúng ta đánh dấu các slides bằng các ký tự A, B, C... như hình
trên thì rõ ràng là D có số thứ tự là 3, B là 1, C là 2 và A là 4.
Nhiệm vụ của bạn là viết một chương trình tự động hóa việc xử lý này.
Input
Input gồm nhiều mô tả heap, mỗi mô tả bắt đầu bằng dòng chứa số nguyên n, số lượng
sildes trong heap. Mỗi dòng trong n dòng tiếp theo chứa 4 số nguyên xmin, xmax, ymin và
ymax cho biết tọa độ của sildes. Các Sildes được đánh dấu A, B, C... theo thứ tự.
N dòng tiếp theo, mỗi dòng chứa 2 số nguyên, tọa độ (x, y) của n số được khắc trên lần
lượt các sildes theo thứ tự input. Không một số nào nằm trên đường biên của silde.
Input kết thúc bởi một mô tả heap có n = 0; không xử lý trường hợp này.
Ouptput
Với mỗi một mô tả heap trong input, trước hết xuất ra số thứ tự của heap. Sau đó in ra
một dãy các slides mà số thứ tự của nó có thể được xác định duy nhất. Sắp xếp các cặp
theo thứ tự A, B, C, ...
Nếu không xác định được một bộ ghép nào thì in ra “none”
Xuất một dòng trắng cho mỗi trường hợp.
DOANSTT.INP DOANSTT.OUT
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
Heap 1
(A,4) (B,1) (C,2) (D,3)
Heap 2
none
solve this problem using maximum matching in c++
Assistant
To solve the problem of determining unique slide assignments in overlapping slides heaps, we can use the concept of bipartite matching. Here, slides are one set of nodes and numbers (representing the order) are another set. We have to assign each number to exactly one slide such that each slide has exactly one number, with minimal operations to deduce the unique assignments.
Detailed Approach:
Input Parsing:
Each heap starts with an integer n, the number of slides.
For each slide, we get its coordinates.
For each number, we get its (x, y) coordinates, which indicate the position of the number on some slide.
Graph Construction:
We construct a bipartite graph where one partition represents the numbers and the other represents the slides.
An edge exists between a number and a slide if the number's (x, y) lies within the slide's boundaries.
Maximum Bipartite Matching:
We use the Hopcroft-Karp algorithm to find the maximum matching. This helps us determine whether a perfect matching exists and how the numbers can be assigned to slides.
For each slide, to check if a number assignment is unique, we temporarily fix the assignment of a number to that slide and check if a perfect matching still exists. If only one possible number can maintain the perfect matching when assigned to a specific slide, then that number must uniquely belong to that slide.
Output:
For each heap, after determining the unique assignments, we print the heap number followed by pairs (Slide, Number) in alphabetical order.
If no unique assignments are found for the heap, we print none.
Implementation:
Here's the C++ implementation using the above approach:
cpp
#include <bits/stdc++.h>
using namespace std;
// Hopcroft-Karp algorithm for bipartite matching
struct HopcroftKarp {
int n, m;
vector<vector<int>> adj;
vector<int> pair_u, pair_v, dist;
HopcroftKarp(int size_u, int size_v) {
n = size_u;
m = size_v;
adj.assign(n, vector<int>());
pair_u.assign(n, -1);
pair_v.assign(m, -1);
dist.assign(n + 1, 0);
}
void add_edge(int u, int v){
adj[u].push_back(v);
}
bool bfs(){
queue<int> q;
for(int u=0; u<n; u++) {
if(pair_u[u]==-1){
dist[u] = 0;
q.push(u);
}
else{
dist[u] = INT32_MAX;
}
}
dist[n] = INT32_MAX;
while(!q.empty()){
int u = q.front(); q.pop();
if(u == n) continue;
for(auto &v : adj[u]){
if(pair_v[v]==-1){
if(dist[n] == INT32_MAX){
dist[n] = dist[u] +1;
}
}
else if(dist[pair_v[v]] == INT32_MAX){
dist[pair_v[v]] = dist[u] +1;
q.push(pair_v[v]);
}
}
}
return dist[n] != INT32_MAX;
}
bool dfs(int u){
if(u != n){
for(auto &v : adj[u]){
if(pair_v[v]==-1 || (dist[pair_v[v]] == dist[u]+1 && dfs(pair_v[v]))){
pair_u[u] = v;
pair_v[v] = u;
return true;
}
}
dist[u] = INT32_MAX;
return false;
}
return true;
}
int max_matching(){
int matching =0;
while(bfs()){
for(int u=0; u<n; u++) {
if(pair_u[u]==-1){
if(dfs(u)){
matching++;
}
}
}
}
return matching;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
int heap_num =1;
while(cin >> n){
if(n==0) break;
// Read slides
struct Slide {
int xmin, xmax, ymin, ymax;
};
vector<Slide> slides(n);
for(int i=0; i<n; i++) cin >> slides[i].xmin >> slides[i].xmax >> slides[i].ymin >> slides[i].ymax;
// Read numbers
vector<pair<int, int>> numbers(n);
for(int i=0; i<n; i++) cin >> numbers[i].first >> numbers[i].second;
// Build adjacency list: numbers on left (0..n-1), slides on right (0..n-1)
vector<vector<int>> adj(n, vector<int>());
for(int num=0; num<n; num++){
int x = numbers[num].first;
int y = numbers[num].second;
for(int s=0; s<n; s++){
if(x > slides[s].xmin && x < slides[s].xmax && y > slides[s].ymin && y < slides[s].ymax){
adj[num].push_back(s);
}
}
}
// Check if initial matching is possible
HopcroftKarp hk(n, n);
hk.adj = adj;
int initial_matching = hk.max_matching();
// If initial_matching <n, no perfect matching, but per problem likely always possible
// Now, for each slide, check if it's uniquely assigned
vector<int> unique_assignment(n, -1); // -1 means not unique
for(int S=0; S<n; S++){
// Find all numbers that can be assigned to S
vector<int> candidates;
for(int num=0; num<n; num++) {
// Check if num can be assigned to S
if(find(adj[num].begin(), adj[num].end(), S) != adj[num].end()){
candidates.push_back(num);
}
}
// Now, for each candidate, check if assigning it to S allows a perfect matching
vector<int> possible_x;
for(auto &x : candidates){
// Assign x to S, and check if the rest can be matched
// Build adj_mod: numbers 0..n-1 excluding x, slides 0..n-1 excluding S
int m = n-1;
// Mapping for numbers: y in 0..n-1 excluding x
// Mapping for slides: t in 0..n-1 excluding S
// Create adj_mod
vector<vector<int>> adj_mod(m, vector<int>());
for(int y=0; y<n; y++){
if(y ==x) continue;
// Map y to y'
int y_prime = (y <x) ? y : y-1;
for(auto &t : adj[y]){
if(t == S) continue;
// Map t to t'
int t_prime = (t < S) ? t : t-1;
adj_mod[y_prime].push_back(t_prime);
}
}
// Now, perform Hopcroft-Karp on adj_mod
HopcroftKarp hk_mod(m, m);
hk_mod.adj = adj_mod;
int matching_mod = hk_mod.max_matching();
if(matching_mod == m){
possible_x.push_back(x);
}
}
if(possible_x.size() ==1){
unique_assignment[S] = possible_x[0];
}
}
// Now, prepare output
cout << "Heap " << heap_num++ << "\n";
// Collect slides with unique assignments
vector<pair<char, int>> assigned;
for(int S=0; S<n; S++) {
if(unique_assignment[S]!=-1){
assigned.emplace_back(make_pair('A' + S, unique_assignment[S]+1));
}
}
if(assigned.empty()) cout << "none\n";
else{
// Sort assigned by slide label
sort(assigned.begin(), assigned.end());
// Print in the format (A,4) etc.
bool first = true;
for(auto &[c, num]: assigned){
if(!first) cout << " ";
first = false;
cout << "(" << c << "," << num << ")";
}
cout << "\n";
}
cout << "\n";
}
}