int n, matchX[MAXN], matchY[MAXN], dist[MAXN], tests, old_matchX[MAXN];
pii a[MAXN];
pair<int, int> b[MAXN];
queue q;
vector adj[MAXN], g[MAXN];
bool findPath() {
for (int i = 1; i <= n; ++i) {
if (matchX[i] == 0) {
q.push(i);
dist[i] = 0;
} else dist[i] = -1;
}
code
bool ok = false;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < int(adj[u].size()); ++i) {
int v = adj[u][i];
if (matchY[v] == 0) {
ok = true;
} else {
if (dist[matchY[v]] < 0) {
dist[matchY[v]] = dist[u] + 1;
q.push(matchY[v]);
}
}
}
}
return ok;
}
bool match(int u) {
for (int i = 0; i < int(adj[u].size()); ++i) {
int v = adj[u][i];
if (matchY[v] == 0) {
matchX[u] = v;
matchY[v] = u;
return true;
} else {
if ((dist[matchY[v]] == dist[u] + 1) && match(matchY[v])) {
matchX[u] = v;
matchY[v] = u;
return true;
}
}
}
return false;
}
void solve() {
for (int i = 1; i <= n; ++i) {
cin >> a[i].x1 >> a[i].x2 >> a[i].y1 >> a[i].y2;
}
code
Assistant
for (int i = 1; i <= n; ++i) {
cin >> b[i].first >> b[i].second;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (a[i].x1 < b[j].first && b[j].first < a[i].x2 && a[i].y1 < b[j].second && b[j].second < a[i].y2) {
adj[i].push_back(j);
g[i].push_back(j);
}
}
}
while (findPath()) {
for (int i = 1; i <= n; ++i)
if (matchX[i] == 0) match(i);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (matchX[i] != 0) ++ans;
old_matchX[i] = matchX[i];
}
cout << "Heap " << tests << '\n';
bool ok = false;
for (int i = 1; i <= n; ++i) {
memset(matchX, 0, sizeof(matchX));
memset(matchY, 0, sizeof(matchY));
memset(dist, 0, sizeof(dist));
if (old_matchX[i] == 0) continue;
for (int i = 1; i <= n; ++i) adj[i].clear();
for (int u = 1; u <= n; ++u) {
for (int v : g[u]) {
if (u == i && v == old_matchX[u]) continue;
adj[u].push_back(v);
}
}
while (findPath()) {
for (int i = 1; i <= n; ++i)
if (matchX[i] == 0) match(i);
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (matchX[i] != 0) ++cnt;
}
if (cnt != ans) {
cout << '(' << char('A' + i - 1) << ',' << old_matchX[i] << ") ";
ok = true;
}
}
if (ok == false) cout << "none";
cout <<"\n\n";
for (int i = 1; i <= n; ++i) {
g[i].clear();
adj[i].clear();
}
memset(matchX, 0, sizeof(matchX));
memset(matchY, 0, sizeof(matchY));
memset(dist, 0, sizeof(dist));
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
Certainly! Here's a detailed explanation of the provided C++ code, breaking down its structure, functionality, and purpose step by step.
Overview
The code implements a bipartite matching algorithm, specifically the Hopcroft-Karp algorithm, to solve a problem involving matching rectangles to points. Additionally, it identifies critical matches—pairs whose removal would decrease the total number of matches. The program processes multiple test cases, reading input from a file and writing output to another.
Key Components
Libraries and Namespaces:
#include <bits/stdc++.h>: Includes all standard C++ libraries.
using namespace std;: Allows usage of the standard namespace without prefixing std::.
Type Definitions and Constants:
typedef long long ll;: Defines ll as a shorthand for long long.
const int INF = 1e9 + 5; and const ll oo = 1e18 + 5;: Define large constants used for initializing distances or representing infinity.
const int MAXN = 105;: Sets the maximum number of nodes (105) for arrays.
Preprocessor Directives:
#define y1 asdsdfg: Redefines y1 to avoid potential conflicts with other libraries or macros.
#define int ll: Globally redefines int to long long to handle larger integers throughout the code.
Data Structures:
Struct pii: Represents a rectangle with coordinates (x1, y1) for the bottom-left and (x2, y2) for the top-right corners.
cpp
struct pii {
int x1, x2, y1, y2;
};
Variables:
int n: Number of rectangles and points.
matchX[MAXN] and matchY[MAXN]: Arrays to store the current matching from X to Y and vice versa.
dist[MAXN]: Array to store distances during BFS in the Hopcroft-Karp algorithm.
tests: Counter for the number of test cases processed.
old_matchX[MAXN]: Stores the previous matching to identify critical matches.
pii a[MAXN]: Array of rectangles.
pair<int, int> b[MAXN]: Array of points.
queue<int> q: Queue used in BFS.
vector<int> adj[MAXN], g[MAXN]: Adjacency lists for the bipartite graph.
Functions
findPath() Function:
Implements the BFS part of the Hopcroft-Karp algorithm to find augmenting paths.
Initializes the queue with all free (unmatched) nodes from set X.
Updates distances and checks if there are paths that can increase the matching size.
Returns true if an augmenting path is found, allowing the matching to be increased.
match(int u) Function:
Implements the DFS part of the Hopcroft-Karp algorithm to find and establish matches.
Attempts to match node u from set X with a node in set Y.
Recursively matches nodes and updates the matchX and matchY arrays.
Returns true if a match is successfully made.
solve() Function:
Input Processing:
Reads n rectangles (a[i]) with their coordinates.
Reads n points (b[i]) with their coordinates.
Building the Bipartite Graph:
For each rectangle a[i] and point b[j], checks if the point lies strictly inside the rectangle.
If so, adds an edge from i (in set X) to j (in set Y) in both adj and g adjacency lists.
Finding Maximum Matching:
Uses the Hopcroft-Karp algorithm (findPath and match functions) to find the maximum number of matching pairs.
Counts the total matches (ans) and stores the current matching in old_matchX.
Outputting Initial Matching:
Prints "Heap " followed by the test case number.
Identifying Critical Matches:
Iterates through each matched pair in old_matchX.
For each pair, temporarily removes the match and recalculates the maximum matching.
If the total matching size decreases (cnt != ans), the pair is critical and is printed.
If no critical matches are found, prints "none".
Workflow Summary
Initialization:
For each test case, the program reads the number of rectangles/points (n).
Input Reading:
Reads the coordinates of n rectangles and n points.
Graph Construction:
Constructs a bipartite graph where rectangles form set X and points form set Y.
An edge exists between rectangle i and point j if point j lies inside rectangle i.
Maximum Matching:
Applies the Hopcroft-Karp algorithm to find the maximum matching in the bipartite graph.
Counts the total number of matches (ans).
Critical Match Identification:
For each matched pair (A, j), checks if removing this pair reduces the total matching count.
Prints such critical pairs or "none" if no critical matches exist.
Output:
Outputs results in a specified format for each test case, indicating the heap number and critical matches.
Termination:
The program continues processing until a test case with n = 0 is encountered, signaling the end of input.
Example Output Format
For each test case, the output begins with "Heap " followed by the test case number. Then, it lists critical matches in the format (A, j) where A is a letter representing the rectangle and j is the matched point number. If there are no critical matches, it prints "none". Each test case's output is separated by two newline characters.
Example:
code
Heap 1
(A,2) (C,3)
Heap 2
none
Conclusion
This C++ program effectively models and solves a matching problem between rectangles and points using the Hopcroft-Karp algorithm. It not only finds the maximum number of matches but also identifies which specific matches are critical to maintaining the maximum matching size. The program is structured to handle multiple test cases efficiently, providing clear and formatted output for each scenario.