We need to understand the problem: There are n existing snow drifts at integer coordinates. Bajtek can move by pushing off from a snow drift in one of the four cardinal directions (north, east, south, west) and sliding until he lands in another snow drift. That means: from a point (x, y), if you go north (increase y), you continue moving in a straight line until you encounter another snow drift at the same x coordinate and with y' > y (the nearest one in that direction). Similarly for other directions. If there is no snow drift in that direction, then you slide infinitely? Actually, "sliding until he lands in another snow drift" implies that you must land in a snow drift; if there is none, you cannot stop? The typical interpretation in such problems (like "ice skating" problems) is that you can only make a move if there is a snow drift in that direction along the same row/column; you slide and stop exactly at that drift. So the graph is defined: there is a directed? Actually, it's undirected? Let's think: If from A you can slide north to B, then from B you can slide south to A (provided there is no other drift in between? Actually, if B is directly north of A, then sliding south from B would encounter A if A is the nearest in that direction; but if there is a drift C between A and B, then A cannot slide directly to B because you stop at the first drift. So it's essentially a graph where vertices are drifts, and there is an edge between two drifts if they are on the same row (same y) or same column (same x) and there is no other drift between them. But note: The ability to slide from A to B requires that no other drift lies on the segment between them. However, typical problems often simplify to: you can move from any drift to any other drift that shares the same x or y coordinate, because you can slide and stop at any drift along that line, not necessarily the nearest? But the problem statement: "pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift." That implies you slide in that direction until you hit another drift. So you cannot control stopping at an intermediate point; you must slide until you encounter the next drift. So it's the nearest drift in that direction. So the graph is based on nearest neighbor in each direction.
But there is nuance: The problem asks to add minimal number of new drifts (at integer coordinates) so that the graph becomes connected (any drift reachable from any other by a sequence of such moves). The addition of drifts can be placed anywhere (integer coordinates). After adding drifts, the graph changes: edges become defined based on all drifts (existing + new). We want to add as few new drifts as possible to make the graph connected.
This is a classic problem: "Ice Skating" from Codeforces? Indeed, there is a known Codeforces problem: "A. Ice Skating" (CF Round #??). I recall a problem: There are n points, you can move from one to another if they share x or y coordinate. But in that problem, you can slide in a straight line to any other point sharing x or y, not just nearest. Actually, I think the CF problem is: "Bajtek is learning to skate on ice. He's a beginner, so his only move is to take a snow drift and push off to the north, east, south or west, after which he slides until he hits another snow drift. He notices that it's impossible to get from some drifts to others. He wants to create some additional drifts to make it possible to travel between any two drifts. Find minimal number of drifts to add." That is exactly the problem. And the typical solution is to treat each existing drift as a node, and connect two nodes if they share the same x or y coordinate (i.e., they are aligned). Then the number of additional drifts needed is (number of connected components - 1). Because you can add a drift at the intersection of a line from one component and a line from another component, effectively joining them.
But wait: In the classic problem, the rule is: you can move from one drift to another if they have the same x-coordinate or same y-coordinate, regardless of obstacles? I recall the problem: "Ice skating" from Codeforces problem 217A? Actually, it might be Codeforces Round #104 (Div. 2) problem A: "Blackjack"? No. There's a known problem: "A. Ice Skating" from Codeforces Round #... The description: "Bajtek is learning to skate on ice. He's a beginner, so his only move is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created." Yes, that is a known problem. And the solution: count connected components based on sharing x or y coordinates, answer = components - 1.
But why does that work? Because if two drifts share the same x or y, you can directly slide from one to the other? Actually, consider drifts at (2,1) and (2,3). If you push north from (2,1), you will slide north until you encounter another drift. If there is a drift at (2,3) and no drift at (2,2), then from (2,1) going north, you will slide and hit (2,3). Similarly, from (2,3) going south, you hit (2,1). So there is an edge. If there is a drift at (2,2) as well, then from (2,1) north you hit (2,2), not (2,3). So it might be that two drifts share same x but there is an intermediate drift. Then you cannot directly slide from (2,1) to (2,3) in one move, but you can go (2,1)->(2,2)->(2,3). So still they are connected via intermediate drifts. So connectivity in the graph defined by "there is an edge between any two drifts that share the same x or y" yields the same connectivity as the actual sliding rules because you can always move along a line, stepping through intermediate drifts. However, caution: If there are drifts that share same x but there is no direct line-of-sight because intermediate drifts block? Actually, they don't block; they provide steps. So the reachable set is exactly the connected components of the graph where vertices are drifts, and edges exist if they share x or y (i.e., lie on same row or column). Because if two drifts share x or y, you can always move from one to the other via a sequence of moves along that line, as long as there is a path of overlapping lines? Wait, suppose we have two drifts that do not share x or y directly, but there is a third drift that shares x with the first and y with the second. Then you can go: first -> third (if they share x or y) -> second. So connectivity is defined by the graph where edges exist between drifts that share the same x or same y. This graph is undirected; each component is a set of drifts that are connected via such edges. And the actual sliding moves allow you to travel within a component because you can move stepwise along aligned drifts.
Hence the problem reduces to: given n points, we consider graph with edges if points share x or y. Find number of connected components. Minimal number of added points to make graph connected is (components - 1). Because you can add a point at an intersection: choose one point from component A and one from component B. If you add a point at (x_A, y_B) (if that coordinate is not already occupied), then that new point shares x with the point from A (if that point's x is x_A) and shares y with point from B (if that point's y is y_B). So then A and B become connected through the new point. Add one point to connect two components, so components-1 needed. But is it always possible to add a point at such an integer coordinate without conflicting? Possibly the intersection may already have a drift. But we can always find some coordinate? Actually, we might need to add a point that connects two components, and we can place it anywhere. The typical reasoning: we can always add a point that connects two components because we can choose a point with a new x or y coordinate? But the requirement is to minimize number of added drifts, and the lower bound is components-1 because each new drift can at best reduce the number of components by 1 (by connecting two components). And we can achieve components-1 by adding a drift at an intersection between a row from one component and a column from another. But what if all rows and columns are occupied in such a way that any such intersection is already occupied? Could that happen? If two components are separate, they have no point sharing x or y. That means the set of x-coordinates in component A and the set of y-coordinates in component B are disjoint from something? Actually, to connect two components, we need to create a point that shares x with some point in A and y with some point in B. If such point already exists, then that point would be in either A or B? If a point exists at (x_A, y_B) and it is in some component, would it connect A and B? Suppose we have component A and component B, with no connections. If there is a point P at (x_A, y_B) that is not in A or B (maybe it's in another component C, or isolated). If P shares x with a point in A, then there would be an edge between P and that point, connecting A and P. Similarly, if P shares y with a point in B, then P connects to B. So if P exists and is connected to both A and B, then A and B would be connected through P, contradicting they are separate components. So such a point cannot exist in any component other than A or B unless it already connects them. So the intersection point (x_A, y_B) is not occupied (unless it belongs to A or B, but if it belongs to A, then it shares y with B? Wait, if it belongs to A, then it would have y = y_B, but all points in A may have various y's. If a point in A has y = y_B and x = x_A, then that point would share y with B's point? Actually, B's point has y = y_B? No, to connect we need a point that shares x with a point in A and y with a point in B. If the point (x_A, y_B) is in A, then it shares x with the A point (itself) trivially, and shares y with some point in B only if there exists a point in B with y = y_B. That is the condition: we choose a point in B that has y = y_B. So if we pick a point (x_B, y_B) from B, and a point (x_A, y_A) from A, then we consider adding (x_A, y_B). If (x_A, y_B) is already in A, then it means there is a point in A with that coordinate. But then does that point connect to B? It would share y with B's point (since y = y_B) so then there would be an edge between that point in A and the point in B (since they share y). Thus A and B would already be connected. Contradiction. So such intersection cannot be occupied by any existing drift, otherwise the two components would be connected already. Therefore, we can always place a drift at (x_A, y_B) to connect A and B. So adding one drift per connection is feasible.
Thus answer = number of connected components - 1.
But is there any nuance that the added drift must be at integer coordinates? Yes, and (x_A, y_B) are integers because coordinates are integers. So fine.
Now verify with given examples:
Example1:
2 points: (2,1) and (1,2). They do not share x or y: x: 2 vs 1, y:1 vs 2. So they are separate components (2 components). Answer = 2-1 = 1. Output 1. matches.
Example2:
2 points: (2,1) and (4,1). They share y=1. So they are in same component. Answer = 1-1 = 0. Output 0. matches.
Thus solution: Count connected components of graph where vertices are points, edges exist if they have same x or y. Output components - 1.
Implementation: n up to 100, simple O(n^2) to build adjacency (for each pair, if x_i==x_j or y_i==y_j, then they are connected). Then DFS or Union-Find to count components.
But wait: There's a nuance: In the actual sliding rules, can you move from a point to another point that shares x or y but there is another point in between? As argued, you can still move stepwise, so connectivity holds. So yes.
Thus the solution is straightforward.
But I'm suspicious: Could there be a scenario where you need more than components-1? Suppose components are such that to connect them you may need more than one new drift if no suitable intersection? But as argued, intersection works. However, what about the requirement that you slide until you land in another snow drift. When you add a new drift at (x_A, y_B), can you actually slide from a point in A to this new drift? For a point (x_A, y_A) in A, if you go north/south/east/west, you will slide until you hit the nearest drift in that direction. To reach (x_A, y_B), you need to be able to slide from (x_A, y_A) to (x_A, y_B) directly. That would require that there is no other drift on the line segment between them. Could there be another drift in A on that column that lies between (x_A, y_A) and (x_A, y_B)? Possibly. For example, component A may have multiple points on the same column x = x_A. Suppose A contains points at (x_A, 1) and (x_A, 5). Then if we pick y_B = 3, and we want to connect via (x_A, 3). From (x_A,1) going north, the nearest drift might be (x_A,5) if there is no (x_A,3). If we add (x_A,3), then from (x_A,1) going north, you would encounter (x_A,3) first (since 3 is between 1 and 5). So that's fine: you can reach the new drift. But from (x_A,5) going south, you would encounter (x_A,3) first (if there is no other between). So you can reach the new drift. However, if A has points with the same x but there is a point at (x_A,2) already, then adding (x_A,3) might be reachable from (x_A,2)? Actually, from (x_A,1) north, nearest is (x_A,2) not (x_A,3) because (x_A,2) is closer. So you would not directly slide to (x_A,3) from (x_A,1). But you could go (x_A,1) -> (x_A,2) -> (x_A,3) stepwise. So connectivity still holds: after adding (x_A,3), you can still reach it via intermediate steps. So yes.
But is there any scenario where adding a single point at (x_A, y_B) fails to connect the two components because the new point might be isolated? For it to be connected, it must share x with some point in A and share y with some point in B. That ensures there is an edge (or potential sliding) between the new point and at least one point in each component. But sliding must be possible: from a point in A to the new point, they must be on same row or column. Since we chose x_A from A, the new point shares x with that A point. So there is a direct alignment. However, as noted, there might be other points between them that could block direct sliding? Actually, sliding is defined as: from a drift you push off in a direction and slide until you land in another drift. So if there is a point in A at (x_A, y_A) and we add point at (x_A, y_B), then from (x_A, y_A) if you go north/south depending on whether y_B > y_A or y_B < y_A, you will slide along the column x = x_A. You will stop at the first drift encountered in that direction. If there is any other drift on that column between y_A and y_B, you will stop at that intermediate drift instead of reaching (x_A, y_B). But does that prevent connectivity? You could then from that intermediate drift continue in the same direction? However, the rules: each move you push off and slide until you land in a drift. You cannot stop midway. So to go from (x_A, y_A) to (x_A, y_B), you need a sequence of moves, not necessarily one move. If there is an intermediate drift, you can go from (x_A, y_A) to that intermediate drift (the nearest), then from that intermediate drift continue in the same direction to eventually reach (x_A, y_B) if there are no other drifts in between that intermediate and the target. But note: after landing at the intermediate drift, you can push off again in the same direction. That is allowed. So you can still traverse stepwise along the column, moving from one drift to the next in order of increasing/decreasing y. So as long as the set of drifts on that column includes a chain covering from y_A to y_B, you can travel. The new point will be part of that chain. But if there is already a drift at (x_A, y_C) between y_A and y_B, then after adding (x_A, y_B), the chain becomes ... (x_A, y_A), (x_A, y_C), (x_A, y_B) possibly. So you can go (x_A, y_A) -> (x_A, y_C) -> (x_A, y_B). So still reachable. So connectivity is not an issue.
But wait: what if the point from A we selected is not the one that shares x? Actually, we said we choose a point from A with x-coordinate x_A (any point in A). But if we pick a point (x_A, y_A) in A, then adding (x_A, y_B) will definitely connect to A because there is a path along column x_A from that point to the new point (maybe via intermediate drifts on same column). However, it might not connect all points in A? The new point is connected to that specific point, and since A is a component, all points in A are connected to that point, so the new point will be connected to all A through existing connections. Similarly for B via y coordinate. So yes.
Thus the standard solution stands.
Now, the problem constraints: n up to 100, coordinates up to 1000. So trivial.
We'll implement using union-find: iterate all pairs i<j, if x[i]==x[j] or y[i]==y[j], union(i,j). Then count distinct roots, answer = components - 1.
Let's test with a custom case: n=3, points (1,1), (2,2), (3,3). No two share x or y, so three components => answer 2. Is that minimal? Could we add one drift to connect all three? If we add drift at (1,2) then it connects (1,1) via x=1 and (2,2) via y=2, and also (3,3) not directly. Then component of (1,1) and (2,2) merges with new drift, but (3,3) remains separate. So need at least 2. Could we add (1,3) and (2,1) maybe? But one drift cannot connect three separate components unless it shares x with one and y with another, but then third remains. So answer 2 is correct.
Thus solution.
Now, is there any possibility that the answer is 0 when n=1? components=1 => 0. That's fine.
Hence code.
But before finalizing, double-check problem statement: "We assume that Bajtek can only heap up snow drifts at integer coordinates." That is fine.
Now the input: first line n, then n lines each with xi yi. All distinct.
Output: minimal number.
Thus solution.
We'll implement in C++.
Implementation details:
- Read n.
- Arrays x[100], y[100].
- Use DSU: parent[100], size maybe.
- Initialize parent[i]=i.
- For i in 0..n-1, for j in i+1..n-1: if x[i]==x[j] or y[i]==y[j], union(i,j).
- Count distinct roots: for i=0..n-1, if parent[i]==i (after find), count++.
- answer = count - 1.
- Print answer.
Potential pitfalls: Ensure DSU with path compression. Or simple DFS.
Thus answer.
Now the user might be expecting Python? The question didn't specify language, but we can provide code in Python for clarity. However, the context is a typical programming problem; we can output solution in C++ or Python. I'll provide both, but since the instructions say "You are an AI", perhaps they just want explanation. The user query: "Bajtek is learning to skate..." likely expects the solution. Usually we provide code. I'll write a solution in Python for simplicity.
But note: The problem is from Codeforces, and typical solutions are in C++ or Python. I'll give Python 3 code.
Edge Cases:
- n=1: components=1, answer=0.
- All points aligned: components=1.
- Many points but disconnected components: answer = components-1.
Complexity O(n^2) fine.
Thus final.
But wait, some may think that we need to consider that when adding a drift, you can't place it on an existing drift, but we argued it's always possible to place at an intersection not occupied. So fine.
Double-check: Is there any scenario where adding a drift at (x_A, y_B) may not be sufficient to connect components because after adding, the connectivity might require more steps? But as argued, the new drift will be adjacent (by sliding) to at least one point in A and one point in B, merging the components. However, is it guaranteed that from a point in A to the new drift there is a sliding move? They share same x, so from that point in A, if you go in the direction towards the new drift (north/south), you will slide until you hit the nearest drift in that direction. It could be that there is another drift from A on the same column that lies between them, so the nearest drift is not the new one. But then you cannot directly slide to the new drift in one move. However, you can still reach it via that intermediate drift: from the point in A you go to the intermediate, then continue. So eventually you can reach the new drift. Therefore, the new drift is reachable from A (there exists a path). So it will be in the same connected component. So union-find after adding would connect.
Thus answer is correct.
But the problem might have a twist: what if the added drift is placed at some coordinate and it might create new drifts that block existing connections? Actually, adding a drift can only add edges, not remove any. So connectivity can only improve. So adding drifts can only reduce number of components.
Hence lower bound is components-1.
Now let's finalize.
I'll produce code.
But I'll also include explanation.
For completeness, we might write solution with DSU.
Python code:
def main():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
x = [0]*n
y = [0]*n
for i in range(n):
x[i] = int(next(it))
y[i] = int(next(it))
parent = list(range(n))
def find(u):
while parent[u]!=u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
def union(u,v):
ru = find(u)
rv = find(v)
if ru!=rv:
parent[rv] = ru
for i in range(n):
for j in range(i+1,n):
if x[i]==x[j] or y[i]==y[j]:
union(i,j)
components = sum(1 for i in range(n) if parent[i]==i) # after path compression? better to find each
# Use find for each i to be sure
roots = set()
for i in range(n):
roots.add(find(i))
print(len(roots)-1)
if name=="main":
main()
That prints answer.
All good.
Now, the user may just want answer; but we produce final solution.
Thus final. The problem reduces to finding the number of connected components in a graph where vertices are the snow drifts, and an edge exists between two drifts if they share the same x-coordinate or the same y-coordinate. The minimal number of additional drifts needed to make the graph connected is then the number of components minus one.
This is because we can always connect two components by placing a new drift at the intersection of the x-coordinate of a drift from one component and the y-coordinate of a drift from the other component. Such a point is guaranteed to be free (otherwise the components would already be connected), and it creates a path between the components. Adding one drift connects two components, so after adding (components - 1) drifts the whole graph becomes connected.
Implementation
We can use Union-Find (Disjoint Set Union) to count the components efficiently:
- Read
n and the coordinates of the n drifts.
- Initialize a DSU structure for
n elements.
- For every pair of drifts
i and j, if they have the same x or the same y, union their sets.
- After processing all pairs, count the number of distinct roots.
- Output
components - 1.
Time complexity: (O(n^2)) (which is fine for (n \le 100)).
Space complexity: (O(n)).
Here is a Python implementation:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
x = [0] * n
y = [0] * n
for i in range(n):
x[i] = int(next(it))
y[i] = int(next(it))
parent = list(range(n))
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
def union(u, v):
ru, rv = find(u), find(v)
if ru != rv:
parent[rv] = ru
for i in range(n):
for j in range(i + 1, n):
if x[i] == x[j] or y[i] == y[j]:
union(i, j)
roots = set()
for i in range(n):
roots.add(find(i))
print(len(roots) - 1)
if __name__ == "__main__":
main()