Sure! Below is a C++ solution to the problem, organized into two functions for clarity and optimized for readability.
Explanation:
-
Function countClosedFigures:
- Purpose: Computes the number of closed figures based on the number of edges (E), vertices (V), and connected components (C) using Euler's formula for planar graphs.
- Formula: The number of closed figures (faces excluding the infinite face) is
E - V + C.
-
Function getGraphInfo:
- Purpose: Processes the input line segments to build the graph, map unique points to unique IDs, and determine the number of unique vertices (V) and connected components (C).
- Implementation:
- Uses a hash map (
std::map) to assign a unique ID to each unique point.
- Implements Union-Find (Disjoint Set Union) to efficiently merge connected components and count the total number of connected components.
-
main Function:
- Reads the input.
- Calls
getGraphInfo to obtain V and C.
- Calls
countClosedFigures with E, V, and C to get the final answer.
- Outputs the result.
C++ Code:
#include <bits/stdc++.h>
using namespace std;
// Function to count closed figures using Euler's formula
int countClosedFigures(int E, int V, int C){
return E - V + C;
}
// Function to process the graph and return number of vertices and connected components
pair<int, int> getGraphInfo(int N, vector<pair<pair<int, int>, pair<int, int>>> &segments){
// Map each point to a unique ID
map<pair<int, int>, int> pointToID;
int id = 0;
for(auto &seg : segments){
pair<int, int> p1 = seg.first;
pair<int, int> p2 = seg.second;
if(pointToID.find(p1) == pointToID.end()){
pointToID[p1] = id++;
}
if(pointToID.find(p2) == pointToID.end()){
pointToID[p2] = id++;
}
}
int V = pointToID.size();
// Initialize Union-Find
vector<int> parent(id);
for(int i=0;i<id;i++) parent[i] = i;
// Find function with path compression
function<int(int)> find_set = [&](int x) -> int{
return parent[x] == x ? x : parent[x] = find_set(parent[x]);
};
// Union function
auto union_set = [&](int x, int y) -> void{
int fx = find_set(x);
int fy = find_set(y);
if(fx != fy){
parent[fx] = fy;
}
};
// Union all segments
for(auto &seg : segments){
int u = pointToID[seg.first];
int v = pointToID[seg.second];
union_set(u, v);
}
// Count connected components
set<int> components;
for(int i=0;i<id;i++) components.insert(find_set(i));
int C = components.size();
return {V, C};
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<pair<pair<int, int>, pair<int, int>>> segments(N, { {0,0}, {0,0} });
for(int i=0;i<N;i++){
cin >> segments[i].first.first >> segments[i].first.second >> segments[i].second.first >> segments[i].second.second;
}
pair<int, int> graphInfo = getGraphInfo(N, segments);
int V = graphInfo.first;
int C = graphInfo.second;
int E = N;
int closedFigures = countClosedFigures(E, V, C);
cout << closedFigures;
}
How It Works:
- Mapping Points to IDs: Each unique point
(x, y) is mapped to a unique integer ID to simplify the Union-Find operations.
- Union-Find Operations: For each line segment, we union the two endpoints. After processing all segments, the number of unique parents in the Union-Find structure gives the number of connected components.
- Applying Euler's Formula: Using the counts of edges, vertices, and connected components, we compute the number of closed figures.
Example:
Input 1:
15
0 0 1 0
3 1 3 3
3 1 3 1
5 4 3 4
5 4 6 4
1 0 0 1
1 1 3 1
6 4 6 3
1 3 1 1
4 1 5 1
6 3 6 2
5 1 5 3
3 0 4 0
4 3 4 1
Output 1:
3
Input 2:
15
0 0 6 0
6 0 6 6
6 0 6 0
0 0 0 0
1 1 3 1
3 1 3 4
3 4 1 4
4 5 4 5
5 4 5 5
5 5 4 5
4 5 4 4
0 7 6 7
6 7 3 9
3 9 0 7
Output 2:
4
This solution efficiently processes the graph to determine the number of closed figures while maintaining clarity and readability.