We need to parse the problem carefully.
We have an original graph that was a tree (since it had N nodes and N-1 edges, connected). After many years, only M edges remain (M ≤ N-1). So the current graph is a subgraph of the original tree, consisting of some edges of that tree. It may be disconnected (since M may be less than N-1). Utkarsh wants to know: how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph.
Interpretation: The original graph had edges = all N-1 edges of the tree. So "were not connected via a direct edge" means that in the original tree, there was no edge directly connecting A and B. That is, they were not adjacent in the tree.
We need to count unordered pairs of nodes (A,B) with A<B such that there is no edge between them in the original tree. But note that the original tree had exactly N-1 edges, each connecting two nodes. So the number of unordered pairs that are directly connected in the original tree is exactly N-1. Therefore, total unordered pairs of nodes = C(N,2). So the number of pairs that were not directly connected in original graph = C(N,2) - (N-1). That is independent of the discovered edges? That would be trivial answer = N(N-1)/2 - (N-1) = (N-2)(N-1)/2. For N=4, that would be (2*3)/2=3, matches sample output. But sample input has M=3 edges (which is the original tree? Actually original tree would have N-1=3 edges. The input edges are: 4-2, 2-1, 3-2. That's exactly a tree with edges: 1-2,2-3,2-4. So the discovered graph is the whole original tree, meaning all edges are present. Then the pairs not directly connected are (1,3), (1,4), (3,4) which is 3. So sample output matches that trivial answer.
But then why mention M and the discovered edges? Possibly because we need to use the discovered edges to infer something about the original graph? Or maybe the problem statement is misinterpreted.
Let's re-read: "In his childhood, Utkarsh had a Graph having N nodes and N-1 edges. It was always possible to go from any node to any other node via a path. Now after so many years he has discovered the same Graph in the store room of his house. But many edges of the Graph are now destroyed. Formally now Utkarsh has only M edges of the old Graph. He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph."
It sounds like we are given the original graph (which was a tree), but we don't know which edges were originally present except that we have discovered M edges from it (some of the original edges are now missing). He wants to know the number of unordered pairs (A,B) that were not directly connected in the original graph. But we don't know the full original graph; we only know that it was a tree, and we have a subset of its edges (the ones still present). We need to compute something based on the information from these M edges? Possibly we need to count pairs (A,B) such that we can guarantee that they were not directly connected? Or maybe we need to compute the number of pairs that were not directly connected, given that the original graph was a tree but we only know some of its edges? But the answer would then be dependent on which tree it was? But the problem likely expects a deterministic answer from the given M edges, implying that the answer is the same regardless of which original tree (consistent with discovered edges) is considered. Maybe we need to consider that the original tree is unknown, and we want to count unordered pairs that we can be sure were not directly connected, based on the known edges? That seems more interesting.
Alternatively, maybe the problem actually asks: Given the current graph (which is a subgraph of the original tree, but we don't know which edges were originally present beyond the ones we have), count the number of unordered pairs (A,B) such that A and B are not connected by a direct edge in the original graph, but note that the original graph is unknown? That would be ambiguous.
Let's check typical contest problems: There's a known problem: Given a tree (original) but only some edges remain (maybe missing some), find the number of unordered pairs (u,v) such that there was no edge between them in the original tree? That trivial answer would be C(N,2) - (N-1) independent of missing edges. That seems too trivial for a problem with constraints up to 1e5. And they wouldn't need to input M and edges. So definitely something else is being asked.
Perhaps I misread: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." Could it be that "were not connected via a direct edge" meaning that in the original graph, there was no path? But that would be "direct edge" is confusing. "direct edge" means an edge directly connecting them. So indeed not directly connected.
Maybe the problem is to count pairs (A,B) for which in the original graph, there was no edge between them, but they are connected in the current graph via some path? Or something like that? Let's examine the sample: N=4, M=3, edges given: 4-2,2-1,3-2. That's exactly a tree. The output is 3, which equals C(4,2)-3 = 6-3 = 3. That matches counting pairs without direct edge.
But what if M < N-1? Suppose N=5, original tree had 4 edges. But we only discovered M edges, say M=2, edges (1-2) and (3-4). Then what would be the answer? According to trivial formula, it would be C(5,2)-4 = 10-4=6. But maybe the answer is something else? Let's test if there's any alternative interpretation.
Perhaps they ask: "how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." But since the original graph is unknown beyond discovered edges, we might need to consider all possible original trees that contain the discovered edges as a subset? Then count pairs that are guaranteed not to have a direct edge in every possible original tree? Or maybe count pairs that definitely did not have a direct edge given that the original graph was a tree? Let's think: In a tree, any two nodes are connected by exactly one simple path. A direct edge exists only if they are adjacent in the tree. If we have some edges known, we can infer that those pairs are definitely directly connected originally. For other pairs, we cannot be sure if they were directly connected or not because the original tree might have placed an edge between them or not, as long as the overall structure is a tree containing the given edges. But the problem asks "how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." It sounds like we need to output a number that is fixed based on the original graph, but we don't know the original graph fully. However, perhaps the original graph is exactly the discovered edges? But the discovered edges are only M, and originally there were N-1 edges. So the original graph must have N-1 edges, but we only see M of them. So the original graph is not fully known.
Maybe the problem actually states: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." And the only information we have is the discovered edges. But maybe the answer is the same regardless of which original tree it was, as long as it contains those discovered edges? Let's test with a small example to see if the answer could be determined uniquely.
Take N=4, suppose we discovered M=2 edges: say (1-2) and (2-3). The original tree must have exactly 3 edges. The discovered edges are two. The third original edge could be any of the remaining possible edges that would turn the graph into a tree (connected, no cycles). The possible missing edge: could be (1-3)? That would create a cycle 1-2-3-1? Actually edges: 1-2,2-3, plus 1-3 would create a triangle, which has 3 edges and 3 nodes, but with N=4 we have 4 nodes, so we also need to connect node 4. So the missing edge could be (1-4), (2-4), (3-4), or (1-3)? Wait, we must have exactly N-1=3 edges total, and we have already 2 edges: 1-2,2-3. So we need one more edge, and it must connect the graph so that all 4 nodes are in one tree. Currently nodes 1,2,3 are connected; node 4 is isolated. So the third edge must connect node 4 to any of 1,2,3. So possible original trees: with third edge 1-4, 2-4, or 3-4. So the set of original direct edges is: {1-2,2-3} plus one of {1-4,2-4,3-4}. Now, consider the unordered pairs (A,B) that were not directly connected in the original graph. The pairs are: (1,2) is directly connected; (2,3) is directly connected; (1,3) is not directly connected in any of the possibilities? Actually if the third edge is not 1-3, but 1-3 is not an option because it would not connect node4, and also would create a cycle? If we added 1-3, we would have edges 1-2,2-3,1-3 among nodes 1,2,3, that would create a cycle (a triangle) which is not allowed in a tree because a tree must be acyclic and have exactly N-1 edges. With 3 edges among 3 nodes, that is a cycle, and node4 isolated, so the graph would be disconnected (since node4 isolated) and also would have 3 edges among 3 nodes -> that is a cycle? Actually a tree on 4 nodes must have 3 edges and be connected. If we have edges 1-2,2-3,1-3, that's 3 edges among 3 nodes, which is a complete graph on 3 nodes, which is not a tree (it has a cycle) and node4 isolated => disconnected, so not a tree. So invalid. So the third edge must be one connecting node4.
Thus, the direct edges in original tree are: {1-2,2-3, x-4} where x is 1,2, or 3.
Now, count pairs not directly connected: total pairs C(4,2)=6. Directly connected pairs: {1-2,2-3, x-4}. So not directly connected pairs: the remaining 3 pairs, but which ones? That depends on x. If x=1, then direct edges are 1-2,2-3,1-4, so the missing direct edges are (1,3),(2,4),(3,4). If x=2, direct edges: 1-2,2-3,2-4, missing: (1,3),(1,4),(3,4). If x=3, direct edges: 1-2,2-3,3-4, missing: (1,3),(1,4),(2,4). So in all cases, the set of not-directly-connected pairs is of size 3, but the specific pairs vary. However, the number of such pairs is always 3. Indeed, 6 - (number of direct edges) = 6 - 3 = 3. Since number of direct edges is always N-1 = 3 regardless of which tree, the count of not-directly-connected pairs is fixed: C(N,2) - (N-1). So it's constant.
Thus, if the problem just asks for number of pairs that were not directly connected in the original tree, answer is always N choose 2 minus (N-1). That's trivial and independent of M and the specific edges. So why would they provide M and edges? Possibly to mislead? Or maybe the problem actually asks something else, like count the number of unordered pairs (A,B) such that they were connected via a direct edge in the original graph but that edge is now destroyed (i.e., missing from M)? Or maybe pairs that were connected originally but are not connected now? Let's read again carefully: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." This wording is ambiguous: It could also mean: Among all unordered pairs (A,B), consider those for which in the original graph, there was no direct edge between them. That's it. And indeed the answer is C(N,2)-(N-1). This is too trivial; but maybe they intend that the original graph had N nodes and N-1 edges, i.e., it was a tree, but the discovered graph has only M edges, and we need to count pairs (A,B) such that in the original graph, they were NOT connected via a direct edge, but we also have the condition that we only know the discovered edges? That doesn't change anything.
Wait, maybe the phrase "were not connected via a direct edge in the original graph" could be misinterpreted: It might mean "were not connected" (i.e., there is no path) via a direct edge? That is nonsense.
Alternatively, could it be that the original graph was a complete graph? No, it says N nodes and N-1 edges, and it was always possible to go from any node to any other via a path. That implies it's a connected graph with N-1 edges, thus a tree. So it's a tree.
Given that, the number of edges in the original graph is fixed, so the number of direct connections is fixed. So the answer is trivial.
But maybe the problem wants to count pairs (A,B) such that in the original tree, they were NOT connected via a direct edge AND they are connected in the discovered graph? Or something like that? The sample: discovered graph is the whole tree, so all nodes are connected. The output 3 matches the trivial answer. But we need to check if there is any nuance: Suppose discovered graph is missing some edges, would the answer still be trivial? For example, N=4, M=2, edges: 1-2, 2-3. Then what would be the answer? According to trivial, it's 3. But maybe they expect something else like 4? Let's see if any problem similar exists.
I recall a CodeChef problem maybe "UTKPLC" or something? I'm not sure. Let's search memory: There's a problem "Tree and Edges" or something like that. But maybe it's from a contest: The problem statement might be incomplete? Let's consider another interpretation: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." Could it be that "were not connected" means that there is no path between them in the original graph? But original graph is connected, so that would be none. That can't be.
Maybe it's about the current graph: pairs (A,B) such that in the original graph, there was no direct edge, but now in the discovered graph, they are connected by a path (since many edges are missing, maybe they are still connected via other edges)? That would be something like: Count pairs of nodes that are connected in the current graph but were not adjacent in the original tree. But sample: current graph is whole tree, so all pairs are connected (since tree is connected). Pairs not adjacent in original tree: (1,3),(1,4),(3,4) => 3, matches output. So that matches as well. However, if current graph is not fully connected, then some pairs might not be connected currently. So the count might be smaller. For instance, N=4, M=2 edges: 1-2,2-3 (so current graph: nodes 1,2,3 are connected, node4 isolated). Pairs that are connected in current graph: (1,2) connected, (1,3) connected via path, (2,3) connected, (others: 1-4,2-4,3-4 not connected). Among these connected pairs, which were not directly connected in original tree? Original tree we don't know exactly, but maybe we need to consider that the original tree had N-1 edges, and the missing edges from original are unknown. However, the condition "were not connected via a direct edge in the original graph" refers to the original tree, which is unknown. So we cannot decide for a pair (1,3) whether they were directly connected originally because it depends on which edge was missing. But maybe we can infer that certain pairs definitely were not directly connected because we know some edges? Or maybe the problem actually expects that the original graph is the discovered graph? But then it says many edges are destroyed, and now he has only M edges of the old graph. So the discovered graph is a subset. So the original had more edges.
Wait, maybe the phrase "were not connected via a direct edge in the original graph" might be miswritten and they meant "are not connected via a direct edge in the discovered graph"? That would be: Count pairs that are not directly connected in the discovered graph. Then answer would be C(N,2) - M. For sample N=4 M=3 => 6-3=3, matches. So that could be it: They ask: how many unordered pairs (A,B) exist such that A and B were not connected via a direct edge in the discovered graph. But the phrasing says "original graph", not "discovered". However, in many problems, they ask: given a graph with N nodes and M edges, count number of pairs not directly connected. That is trivial as well: C(N,2)-M. That is also trivial. But at least it uses M. For N up to 1e5, M up to N-1, computing C(N,2)-M is trivial, but they would still require big integer? C(N,2) can be up to ~5e9 which fits in 64-bit. So still trivial.
But why mention the story about original graph being a tree and many edges destroyed? That would be irrelevant if we just need C(N,2)-M. However, they might include the background to justify that the graph was originally a tree, but now it's not necessarily a tree (since edges missing, it becomes forest). But the question is about the original graph's direct edges? Actually if they ask about the original graph, the answer is independent of M; if they ask about the current graph, answer is C(N,2)-M. Both are trivial. Which one is more likely? The sample input: 4 3 edges are the whole tree, both give 3. So ambiguous.
Let's see constraints: M ≤ N-1. That is interesting because in a simple graph with N nodes, M could be up to N(N-1)/2, but they bound M ≤ N-1. So they emphasize that the original was a tree, and current graph is a subgraph of a tree, so it is a forest (since it's acyclic). So M is at most N-1. So computing C(N,2)-M would still be trivial.
But maybe they ask something else: Count unordered pairs (A,B) such that there exists a path between A and B in the original graph but not a direct edge? That would be C(N,2) - (N-1) again, because original is connected. Too trivial.
What about counting pairs (A,B) such that there is a path between them in the current graph? That would be: For each connected component of size s, number of pairs within that component is C(s,2). Sum over components. That's a typical problem: given a forest (with N nodes, M edges, M ≤ N-1), compute number of pairs of nodes that are connected (i.e., there exists a path) in the forest. But the problem statement says: "were not connected via a direct edge in the original graph." That doesn't match.
Let's check typical problem: "Given a tree, some edges are removed, find number of pairs of nodes that are still connected." That would be sum over components C(size,2). But that would use M and edges to compute component sizes. And answer would be something like: total connected pairs. The sample: N=4, M=3, edges given are the full tree, so the forest is connected, component size 4 => C(4,2)=6, not 3. So not that.
What about: "pairs that are not connected via a direct edge in the current graph"? That yields C(4,2)-3=3. That matches sample. So maybe the wording "original graph" is a mistake; they meant "current graph". However, the story says: original graph had N-1 edges; now many edges are destroyed; formally now he has only M edges of the old graph. So the current graph has M edges. He wants to know how many unordered pairs (A,B) exist such that A and B were not connected via a direct edge in the original graph. If he wants to know about original graph, he doesn't need current edges. But perhaps he can't remember the original graph, but he knows that the original graph was a tree. From the remaining edges, he can deduce that some pairs definitely were not directly connected? For example, if two nodes are in different connected components in the current forest, then in the original tree, they were connected via some path that includes destroyed edges. But could they have been directly connected? Possibly yes, because the destroyed edge could be that direct edge. So we cannot be sure. However, perhaps the problem asks: Count unordered pairs (A,B) for which it is guaranteed that they were not directly connected in the original tree, given the known edges? That would be a more interesting problem. Let's test with N=4, M=2 edges: 1-2, 2-3. Which pairs can we be sure were not directly connected originally? We know the original tree had 3 edges, two of which we have: (1-2) and (2-3). The third edge must connect node 4 to either 1,2,3. So we cannot be sure about any pair that involves node4? For pair (1,4): the third edge could be 1-4, in which case they were directly connected; or could be 2-4 or 3-4, in which case they were not directly connected. So we are not sure if (1,4) were directly connected. So they are not guaranteed to be not directly connected. For pair (2,4): could be direct if third edge is 2-4, otherwise not. So not guaranteed. For pair (3,4): similarly not guaranteed. For pair (1,3): In all possible original trees, is there a direct edge between 1 and 3? The third edge always connects node 4; the edges (1-2) and (2-3) exist, so (1,3) are not directly connected because adding any edge connecting node4 does not create edge 1-3. So (1,3) is definitely not directly connected. Also, what about pair (1,2)? They are directly connected (we know that edge exists in original, because it's in the discovered set? Wait, discovered edges are from the original graph, so those edges are definitely present in the original. So pairs that are directly connected in the original include all discovered edges. So they are not "not directly connected". So they shouldn't be counted. Also pair (2,3) is directly connected. So the only pair that is guaranteed not to have a direct edge is (1,3). So answer would be 1. That is not equal to 3. So sample would not match because in sample, with full tree discovered, all edges are known; then pairs guaranteed not to have a direct edge are those not present in discovered edges, which are exactly the three missing edges: (1,3),(1,4),(3,4) but wait discovered edges include (4,2),(2,1),(3,2). So (1,4) is not directly connected? Actually discovered edges: 4-2,2-1,3-2. So (1,4) are not directly connected (since there's no edge 1-4), but is it guaranteed that there was no direct edge originally? Since we know all original edges (since M = N-1, we have the whole tree), we know that (1,4) is not an edge in the original, so it is guaranteed not directly connected. So all three missing pairs are guaranteed not directly connected. That would be 3, matching sample. So in the scenario where M=N-1, the answer would be C(N,2)-M = 3, which matches sample. But for M=2 in N=4, the guaranteed not-directly-connected pairs would be only (1,3) maybe? That yields 1. However, C(N,2)-M = 6-2=4, not 1. So if the problem expected C(N,2)-M, then for M=2 they'd output 4, but from reasoning about guaranteed pairs it's 1. Which one is more likely? Let's see if any known problem counts "pairs that are not adjacent in the original tree given only a subset of its edges". That seems like a reasoning problem.
But maybe there's another twist: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." Could it be interpreted as: He wants to know the number of pairs that are not connected by a direct edge in the original graph, but he only has the current graph. However, maybe he can deduce exactly which pairs were directly connected? Because the original graph is a tree, and from the current forest, he might be able to deduce that some pairs must have been directly connected originally (the discovered edges), but for other pairs, they might or might not have been directly connected. But the question might be: "Given that the original graph is a tree, and we have a subset of its edges, what is the number of unordered pairs (A,B) for which we can be certain that they were NOT directly connected in the original graph?" That is a logical inference problem: find pairs that are definitely not edges in the original tree, given that the original tree contains the given edges as a subset. Because the original tree must be a spanning tree containing those M edges. Among the remaining possible edges (the complement of the given edges), some might be forced to be absent (i.e., cannot be in the original tree) because including them would create a cycle with the given edges? Wait, if we have a set of given edges that are known to be part of the original tree, we need to extend it to a spanning tree with N-1 edges. So we need to add (N-1 - M) more edges from the set of potential edges (i.e., all pairs not in the given set). However, we must avoid cycles. So any edge that would create a cycle with the given edges cannot be added because the original tree is acyclic. Therefore, those edges are definitely not in the original tree. Also, some edges might be forced to be present to achieve connectivity? Actually to get a spanning tree, we need to connect all components. So we must add edges that connect the components. But there may be multiple choices: we could connect components using any edge between them. So edges that are between two different components could be either present or absent in the original tree, depending on which edges we pick to connect components. So such edges are not definitely absent; they are possible. However, edges that are within the same component (i.e., between nodes already connected by the given edges) and not already in the given set: adding such an edge would create a cycle, so they cannot be in the original tree. Therefore, those edges are definitely NOT directly connected in the original graph.
Thus, the guaranteed not-directly-connected pairs are exactly those pairs (A,B) such that:
- They are not among the given M edges (obviously).
- They belong to the same connected component of the current graph (i.e., there is a path between them using only the given edges). Because if they are in the same component, adding an edge between them would create a cycle, which is not allowed in a tree. Since the original tree must be acyclic and already contains the given edges (which connect them via some path), the edge (A,B) cannot be present in the original tree. Therefore, it is guaranteed that they were not directly connected.
If they are in different components, then it's possible that the original tree included a direct edge between them to connect the components (since we need to add exactly (number of components - 1) edges to make it a tree). However, it's also possible that the connection was made via other nodes. So we cannot be certain that they were not directly connected; they might have been, or might not.
Therefore, the number of unordered pairs (A,B) that are definitely not directly connected in the original tree = sum over each connected component of C(size_i, 2) - (number of given edges inside that component). But note that given edges inside the component are exactly (size_i - 1) if the component is a tree? Actually each component is a tree (since it's a forest subgraph of a tree, so each component is a tree). In a tree, number of edges = size_i - 1. But we only have M edges total, but each component that is a tree of size s has exactly s-1 edges (since it's a connected subgraph of a tree? Actually a forest of a tree is a subgraph that is also a forest, but the original tree is a tree; if we remove some edges, we get a forest where each component is a subtree? Actually each component is a tree, but not necessarily spanning all nodes originally in that component? Wait, the original tree is a tree. If we remove some edges, the remaining graph is a forest, and each component is a tree (since it's still acyclic and connected). However, the number of edges in a component of size s is exactly s-1 because it's a tree. Indeed, any connected acyclic graph on s nodes has exactly s-1 edges. Since the original is a tree, any subgraph that is connected and acyclic (which it is, because we only removed edges, not adding, so it remains acyclic) must have at most s-1 edges; but can it have fewer than s-1 edges and still be connected? No, a connected graph on s nodes must have at least s-1 edges, and if it is acyclic, it must have exactly s-1 edges. However, is it guaranteed that each component is connected? The components are by definition connected components of the forest. So each component is connected. Since the forest is acyclic (as a subgraph of a tree), each component is a tree, thus has exactly (size_i - 1) edges. So indeed, the number of given edges within component i is (size_i - 1). Therefore, within each component, all possible edges among its nodes, except the (size_i - 1) edges that are present, are definitely absent from the original tree? Wait, check: In a tree of size s, there are s-1 edges. The given edges in that component are exactly those that remain from the original tree. But are they necessarily all the edges of that component in the original tree? The component is formed by the remaining edges. In the original tree, there were additional edges that were destroyed, some of which might be between nodes in the same component? Actually, careful: The original tree is a single tree containing all nodes. Some edges are removed (destroyed) to get the current forest. The current forest consists of the edges that survived. If we consider a connected component of the forest, it is a set of nodes that are still connected via surviving edges. However, in the original tree, there might have been edges between nodes in this component that are now destroyed. But if such an edge existed, it would create a cycle within the component when combined with the surviving edges? Not necessarily: Suppose the original tree had edges: 1-2, 2-3, 1-3? That would be a cycle, impossible. In a tree, there is exactly one simple path between any two nodes. If the original tree had an edge between two nodes u and v, then that is the unique path between them. If we remove that edge, they might still be connected via some other path? In a tree, removing an edge disconnects the tree into two components. So if we have a component that remains connected after removal of some edges, it must be that we have not removed all edges that connect that component? Let's think: The original tree is a tree. Remove a set of edges (the destroyed ones). The remaining graph is a forest. Each component of this forest corresponds to a connected subgraph after removal. For any two nodes in the same component, there exists a path using only surviving edges. In the original tree, there was also a unique path between them, which might have included some destroyed edges. But could that unique path include a destroyed edge and still they are connected via surviving edges? If the unique path in the original tree uses a destroyed edge, then after removal, that path is broken. However, they could still be connected via some other path? In a tree, there is only one simple path, so if that unique path includes a destroyed edge, then after removal, those two nodes would be disconnected unless there is another path, but there isn't. So they would be in different components. Therefore, if two nodes are in the same component after removal, it means that the unique path between them in the original tree consists entirely of surviving edges. Because if any edge on that path were destroyed, they would be disconnected. Therefore, for any two nodes in the same component, all edges on the unique path between them in the original tree are present in the surviving set. Consequently, the original tree's edge between them (if there was one) is actually part of that path? Actually, if u and v are adjacent in the original tree, then the unique path is just the edge (u,v). That edge would have to be surviving for them to be in the same component. So if u and v are adjacent in the original tree, then that edge must be among the surviving edges. Therefore, if (u,v) is not among the surviving edges, then u and v cannot be adjacent in the original tree. Moreover, if they are in the same component, the edge (u,v) cannot be present because if it were present, it would be surviving (otherwise they'd be in different components). Therefore, for any two nodes in the same component, either they are directly connected by a surviving edge (so that edge is in the original tree), or they are not directly connected in the original tree (since the edge is not present). But could there be a scenario where they are directly connected in the original tree but the edge is destroyed? That would put them in different components because that edge was the only path. So if they are in the same component, they cannot have been directly connected in the original tree with the edge destroyed. So indeed, for any two nodes in the same component, we can be certain that they were NOT directly connected in the original tree, unless the edge between them is actually present in the surviving set. Because if they were directly connected, that edge must be present to keep them connected. So the only direct connections in the original that could be destroyed are those that are between nodes in different components. Therefore, the guaranteed not-directly-connected pairs are exactly those pairs that are in the same component and are not an edge in the surviving graph. But note: within a component, the surviving edges are exactly the edges of the tree that connect that component. As argued, each component is a tree, so it has exactly (size_i - 1) edges. Those edges correspond to pairs that are directly connected in the original tree (since they are present). For any other pair within the same component, they are not directly connected in the original tree. So the number of guaranteed not-directly-connected pairs = sum over components [ C(size_i, 2) - (size_i - 1) ] = sum over components [ (size_i*(size_i-1)/2 - (size_i-1) ) ] = sum over components [ (size_i-1)(size_i/2 - 1) ] maybe simplify: C(s,2) - (s-1) = (s(s-1)/2 - (s-1)) = (s-1)(s/2 - 1) = (s-1)(s-2)/2. Indeed, (s-1)(s-2)/2. Check: C(s,2) - (s-1) = s(s-1)/2 - (s-1) = (s-1)(s/2 - 1) = (s-1)(s-2)/2. Yes. So total = sum_i (size_i - 1)*(size_i - 2)/2.
Now, what about pairs in different components? They may or may not have been directly connected originally. So we cannot guarantee they were not directly connected. Therefore, the answer to the problem (if interpreted as number of pairs that are definitely not directly connected) would be sum over components (size_i-1 choose 2)? Actually (size_i-1)*(size_i-2)/2 = C(size_i-1, 2)? Let's check: C(s-1,2) = (s-1)(s-2)/2. Yes exactly. So answer = sum_i C(s_i - 1, 2).
Now test with sample: N=4, M=3. The forest is one component of size 4. Then answer = C(4-1,2) = C(3,2)=3. Matches.
Test with N=4, M=2 edges: 1-2,2-3. Then components: one component of size 3 (nodes 1,2,3) and one component of size 1 (node4). For size3: C(3-1,2)=C(2,2)=1. For size1: C(0,2)=0. Total =1. That matches our earlier reasoning that only (1,3) is guaranteed not directly connected. So answer would be 1. That is a plausible answer. However, is it too complicated? Let's see constraints: N up to 1e5. We can compute component sizes using DSU or DFS on the forest (since M ≤ N-1, it's a sparse graph). Then compute sum (s_i-1)*(s_i-2)/2. That's O(N+M). That is a reasonable problem. The answer is not trivial like C(N,2)-M, which would be 4, not 1. So it's more interesting.
Now, which interpretation is more likely given the problem statement? The statement: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." If we interpret "were not connected via a direct edge" as "there is no direct edge between them", then we want pairs that are not edges in the original graph. But we don't know the original graph fully; we only know a subset of its edges. However, maybe the problem implicitly assumes that we can determine which pairs definitely lack a direct edge? The statement doesn't mention "definitely" or "guaranteed". It just says "exist such that A and B were not connected via a direct edge in the original graph." That suggests we are counting pairs that actually satisfy that property in the true original graph. But since we don't know the original graph, we cannot compute it. Unless the original graph can be uniquely determined from the discovered edges? But it's not uniquely determined when M < N-1. So the answer would not be uniquely determined; it would depend on which original tree it was. But problem expects a single integer output given the input. Therefore, the original graph must be deducible from the discovered edges. How could it be deducible? If we know it was a tree, and we have a subset of its edges, does that determine the rest? No. So maybe the problem assumes that the discovered edges are exactly the ones that are not destroyed? Actually, "now Utkarsh has only M edges of the old Graph." That means he physically possesses those edges; the others are destroyed (missing). So the original graph had N-1 edges, but we only have M of them. So we know which edges definitely existed (the ones we have). The original graph also had other edges that are now destroyed, but we don't know which ones they were. However, we might be able to infer that some pairs definitely were not edges because if they were edges, they would have been part of the original tree, but would they have to be among the discovered ones? Not necessarily; they could be among the destroyed ones. So we cannot be sure. So if the problem asks for pairs that were not directly connected in the original graph, we cannot compute it without knowing the full original tree. So the only logical interpretation is that the problem asks for the number of unordered pairs (A,B) for which we can be certain that there was no direct edge between them in the original graph, given the information we have (the remaining edges). This is a typical problem: "Given a tree, some edges are known (surviving), determine the number of pairs of vertices that are not adjacent in the original tree." But the original tree is not fully known, so we count pairs that we can be sure are non-adjacent. This is a common type of problem: "You are given a forest (the known edges) that is a subgraph of an unknown tree on N vertices. Count the number of vertex pairs that are guaranteed not to be edges in the original tree." And the solution is sum over components C(size_i-1, 2). I've seen similar problems. Actually, I recall a problem from Codeforces: "Given a tree, some edges are missing, find the number of pairs that are not connected by an edge in the original tree" but that's not exactly.
Let's search memory: There is a problem from CodeChef: "UTKPLC" maybe not. There is a problem named "Tree and Edges" from some contest. But I recall a problem "Friendless Dr. Sheldon Cooper" or something about pairs not connected directly? Not sure.
Another thought: The problem might be asking: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." But maybe he can figure out that originally, there were N-1 edges, and any pair that is not an edge in the current graph could be a candidate. However, given that the original graph was a tree, any two nodes are connected by exactly one path. The direct edge is the only edge that directly connects them. If they are in the same component of the current graph, then they are connected by a path of surviving edges. In the original tree, that path must be the same as the unique path (since tree). Therefore, if they are in the same component, the unique path between them consists entirely of surviving edges, meaning that the direct edge cannot exist (because that would be a different path). So they were not directly connected. If they are in different components, the unique path in the original tree must include at least one destroyed edge, so it's possible that the direct edge is itself destroyed? But if they are in different components, could they have been directly connected? Yes, the destroyed edge could be the direct edge between them. So they might have been directly connected. So from the information, we cannot be sure. Therefore, the number of pairs that we can be sure were not directly connected is exactly the number of pairs within the same component minus the number of edges within that component (which are the direct connections we know about). That's sum_i (C(s_i,2) - (s_i - 1)) = sum_i C(s_i-1,2). So that is the answer.
Given that the sample matches, and the constraints make sense, I think this is the intended problem.
Let's test with another scenario to be sure.
Example: N=5, suppose discovered edges: 1-2, 2-3, 4-5. So M=3 (since N-1=4, so one edge missing from original). So the original tree had 4 edges. We know 3 of them. The missing edge could be any edge that connects the two components (component A: {1,2,3} size3; component B: {4,5} size2) without creating a cycle. The missing edge must be between a node in component A and a node in component B, because adding an edge within a component would create a cycle. So the missing edge is one of the 3*2=6 possible cross edges. So which pairs are guaranteed not directly connected? Within component A: pairs are (1,2) direct (surviving), (2,3) direct, (1,3) not direct. So (1,3) is guaranteed not direct. Within component B: (4,5) direct, so no other pairs. Cross component pairs: (1,4), (1,5), (2,4), (2,5), (3,4), (3,5). Among these, any could be the missing edge. So they are not guaranteed not direct. So guaranteed not direct pairs: just (1,3). That's 1. According to formula: component sizes 3 and 2. Sum C(3-1,2)=C(2,2)=1; C(2-1,2)=C(1,2)=0. Total 1. Yes.
If the original tree were known to be exactly the discovered edges plus some particular missing edge, the total number of not-directly-connected pairs would be C(5,2)-4 = 10-4=6. But that is not uniquely determined. So the problem must be about guaranteed pairs.
Thus, I'm convinced that the intended interpretation is: given the surviving edges of a tree, count the number of pairs of nodes that are definitely not adjacent in the original tree.
Let's read the problem statement again: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." The phrase "exist" could be ambiguous. It might be interpreted as "How many unordered pairs (A,B) are there for which it is the case that A and B were not connected via a direct edge in the original graph?" That is a property of the original graph, which we don't fully know. However, we can deduce that certain pairs definitely satisfy this property, and perhaps the problem expects us to count those. The problem might be from a contest where participants are expected to understand that we are to count pairs that we can be sure about given the information. But the statement does not explicitly say "definitely" or "surely". It just says "exist". That could be misinterpreted. Perhaps the original statement had the word "surely" or "guaranteed". I'm not sure.
Let's check sample: Input: 4 3 edges as given. The output is 3. That matches both interpretations: trivial C(N,2)-M and also the sum formula. But if we try a test where M < N-1, the two interpretations differ. Since the problem setter provided constraints M ≤ N-1, they likely expect to use the forest to compute component sizes. The trivial answer C(N,2)-M would be too easy and they'd probably not ask it.
Also, note that the input edges are the discovered edges. They could be any subset of the original tree's edges. The problem wants us to output a single integer. So I'm leaning towards the interpretation that we need to compute sum over components C(size_i-1, 2). Let's derive that thoroughly.
Proof: The original graph is a tree with N nodes and N-1 edges. The given M edges are a subset of those edges. So the given graph is a forest (acyclic) with M edges. Let there be k components with sizes s1, s2, ..., sk. In each component, the given edges form a tree (since it's acyclic and connected). So each component has exactly s_i - 1 given edges. The original tree had N-1 edges total, so there are (N-1) - M edges missing (destroyed). Those missing edges are exactly the edges of the original tree that are not given. In the original tree, the given edges are present. Now consider any pair of vertices (u,v). If u and v are in the same component, then the unique path between them in the original tree must consist entirely of given edges, because if any edge on that path were missing, they'd be disconnected in the forest (since the forest only contains given edges). Hence the unique path uses only given edges. Therefore, if u and v were adjacent in the original tree, the edge (u,v) would be the unique path and would be given. So if (u,v) is not a given edge, then they cannot be adjacent. Thus, for any pair in the same component that is not a given edge, we are sure they were not directly connected. The number of such pairs per component is C(s_i, 2) - (s_i - 1) = (s_i - 1)(s_i - 2)/2 = C(s_i - 1, 2).
If u and v are in different components, then the unique path in the original tree must include at least one missing edge. It is possible that the edge (u,v) itself is a missing edge, or the path goes through other nodes. So we cannot be certain whether they were directly connected or not.
Therefore, the total number of pairs that we can be sure were not directly connected is sum_i C(s_i - 1, 2).
The problem asks: "how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." Under the interpretation "exist" meaning "there exist such pairs" would be weird. But I think it's implicitly "how many unordered pairs (A, B) can he be sure that were not connected via a direct edge?" Possibly the original statement had the word "surely".
Given typical contest problems, this is a neat combinatorial result. Let's see if there's any other nuance: The problem might also ask to count pairs that are not connected via a direct edge in the original graph, but given that he has only M edges, maybe he wants to know the maximum possible number of such pairs? Or minimum? But the phrase "exist such that" suggests existence, not maximum/minimum.
Maybe it's: "He wants to know how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." Could also be interpreted as: He wants to know, among all unordered pairs, how many of them are not direct edges in the original graph. But he doesn't know the original graph; however, he knows that the original graph was a tree. So maybe he can deduce that any pair that is not an edge in the discovered graph and also does not complete a cycle? But I'm sticking with the "surely" interpretation.
Let's search online memory: I recall a problem "Utkarsh and Party" or something, but not sure. Could be from Codeforces Round? There is a problem "Edges in a Tree" but not exactly.
Another way: Could the answer be sum over components C(s_i,2) - (s_i - 1) = sum_i (s_i - 1)(s_i - 2)/2? That's what we have.
Let's compute for N=1? If N=1, M=0, components: one of size 1, C(0,2)=0. So answer 0. That makes sense: there are no pairs.
If N=2, M could be 0 or 1. If M=1 (original tree edge known), component size 2, answer = C(2-1,2)=C(1,2)=0. Indeed, if we know the edge, the pair (1,2) is directly connected, so we cannot be sure any pair is not directly connected. If M=0, components: two components each size 1, each contributes 0, total 0. So answer 0. But is that correct? With N=2, original tree had exactly one edge. If we have no edges (M=0), then the pair (1,2) might be the missing edge. Could it be that they were directly connected? Yes, because the original tree edge could be (1,2). So we are not sure they were not directly connected. So 0 pairs guaranteed not directly connected. So answer 0. Works.
If N=3, possible M values. Suppose tree original edges: say unknown. Given M=1 edge, say 1-2. Then components: size2 (1,2) and size1 (3). For size2: C(1,2)=0; size1:0; total0. So answer 0. Which matches: we cannot be sure about any pair: (1,3) could be edge or not, (2,3) could be edge or not, (1,2) is edge. So indeed no guaranteed non-edge. If M=2 edges, say 1-2,2-3 (full tree), then component size3: C(2,2)=1. Actually (1,3) is guaranteed not direct. So answer 1. That matches formula.
Thus, the answer is sum_i C(s_i-1,2). We can compute efficiently using DSU to find component sizes. Since M ≤ N-1, the graph is sparse, we can also DFS. Input up to 1e5, so O(N+M) works.
We need to output a single integer. The result may be up to about sum_i (s_i^2) but with N=1e5, the maximum occurs when there is one component of size N, then answer = C(N-1,2) = (N-1)(N-2)/2, which is about 5e9 for N=1e5, fits in 64-bit (since (1e5-1)*(1e5-2)/2 ≈ (1e10)/2 = 5e9 < 2^63-1). So 64-bit integer is enough. Use long long in C++.
Now, we should ensure that the input might include edges that are not necessarily forming a forest? But it says M edges of the old graph, which is a tree, so these edges are from a tree, thus they will not create cycles because they are a subset of a tree's edges. Actually, is it guaranteed that the given M edges are a subset of the original tree? Yes, they are the remaining edges. So they are edges from the tree. Therefore, the given M edges will form a forest (acyclic) because the original tree is acyclic, and removing edges cannot create cycles; it can only break connectivity. So the graph is indeed a forest. So we don't need to check for cycles.
Thus, algorithm:
- Read N, M.
- Build adjacency list or DSU. Since it's a forest, we can just use DSU to union nodes from each edge. This will group components.
- After processing all edges, count sizes of each component: we can have an array size[root] initially 1, accumulate during union, or after unions, loop over nodes and increment count of their find root.
- For each component root with size s, add (s-1)*(s-2)/2 to answer.
- Print answer.
Edge cases: N can be 1, M=0. Then components: one of size 1 => answer 0. Works.
Let's test more:
N=5, M=0 => each node isolated: each component size 1 => answer 0. Indeed, with no edges, we cannot be sure about any pair? Actually, the original tree had 4 edges, but we have no information. For any pair, it could be an edge in the original tree or not. So we are not sure. So answer 0.
N=5, M=4 (full tree) => one component size 5 => answer = C(4,2)=6. Original tree had 4 edges, so non-edges are C(5,2)-4=10-4=6, matches.
N=5, M=3 as earlier gave 1.
Now, consider a case where components are of sizes: 2,2,1. For N=5, M edges: maybe edges: 1-2, 3-4. Then components: {1,2} size2, {3,4} size2, {5} size1. For size2: C(1,2)=0; each contributes 0. Total 0. So answer 0. Check: Known edges: 1-2 and 3-4. Unknown edges: there are 2 missing edges (since original had 4 edges total). The missing edges must connect the three components into a tree. They must be exactly 2 edges that connect the components without cycles. So the original tree could be: connect 5 to one of 1,2,3,4, and connect the two components (1,2) and (3,4) with an edge between some nodes, or connect 5 to both? Actually we need to form a tree: with components of sizes 2,2,1, we need to add 2 edges to connect them into a tree. The possible trees: we could connect 5 to component A (say edge 5-1), and then connect component A and B via an edge (say 1-3). Then the pairs (5,3) are not directly connected? Actually the pairs that are guaranteed not directly connected: within component A: (1,2) is directly connected (known). So no other pair in A. Within component B: (3,4) directly connected. No other. Cross pairs: (1,3), (1,4), (2,3), (2,4), (5,1), (5,2), (5,3), (5,4). Among these, could any be direct? The two added edges are (5,1) and (1,3) in this scenario, so (5,1) and (1,3) are direct. So other cross pairs are not direct. Could (5,3) be direct? It could be if we choose different edges: e.g., add edges (5-3) and (1-3) would give direct (5,3) and (1,3). So (5,3) could be direct. So indeed none of the cross pairs are guaranteed non-direct. So answer 0. Good.
Thus formula works.
Now, let's confirm with another scenario: N=6, M=2 edges: 1-2, 2-3 (component size3) and another isolated nodes 4,5,6 each size1. Then answer = C(3-1,2)=1. So only (1,3) is guaranteed. Good.
If we have two components of size>1: e.g., sizes 3 and 3, M= (3-1)+(3-1)=4 edges, missing edges = (N-1)-M = 5-4=1? Actually N=6, N-1=5, M=4, so one missing edge. That missing edge must connect the two components. Within each component, guaranteed non-edges: each gives C(2,2)=1. So total 2. So pairs: In component1: suppose nodes {1,2,3} with edges 1-2,2-3; guaranteed non-edge (1,3). In component2: nodes {4,5,6} with edges 4-5,5-6; guaranteed non-edge (4,6). So total 2. Cross pairs: (1,4) etc are not guaranteed. So answer 2.
Now check: Could there be any scenario where within a component, there is an edge that is not known? No, because the component is defined by known edges; all edges that are known are present; any other edge within the component would be a non-known edge. But could such an edge be present in the original tree? As argued, no, because it would create a cycle. So it is guaranteed absent.
Thus the solution is correct.
Now, we should also consider if the given edges might not be from the original tree? But problem says they are from the old Graph, i.e., the original graph. So they are a subset of original edges.
Therefore, I'll proceed with solution: find connected components (using DSU) in the given graph (forest). For each component of size s, add (s-1)*(s-2)/2 to answer. Print answer.
Implementation details:
- Use 1-indexed nodes.
- DSU: parent array size N+1, size array.
- For each edge (a,b), union(a,b).
- After all unions, we need to compute sizes of components for each root. We can either maintain size during union, but careful: after path compression, the root may change; but we can just after all unions, loop i=1..N, find root of i, increment count for that root (e.g., using an array cnt[root]++). Then for each root with cnt>0, compute contribution.
Alternatively, we can use adjacency list and DFS to avoid DSU, but DSU is fine.
Complexities: O(N α(N) + M α(N)) ≈ O(N+M).
Edge Cases:
- M could be 0, then each node is its own component of size 1. Then contributions 0.
- N can be large, use 64-bit.
Now let's verify sample with our solution:
N=4, M=3 edges: 4-2,2-1,3-2. DSU unions: all nodes become one component of size 4. Contribution = (4-1)(4-2)/2 = 32/2=3. Output 3. Good.
Thus, solution matches sample.
Now we need to write final answer.
But before finalizing, let's think if any alternative interpretation could yield a different result that also matches the sample but is more plausible. For instance, maybe the problem asks: "how many unordered pairs (A,B) exist such that A and B were not connected via a direct edge in the original graph." If we interpret that as "pairs that are not edges in the original graph", then answer = C(N,2)-(N-1) always. That is a constant independent of input, so why provide M and edges? Unless the problem originally had to compute something else but the sample still coincidentally matches because M=N-1. But a typical contest problem would not ask such a trivial constant. Also, constraints M ≤ N-1 indicates they expect you to read M edges. So it's not constant.
What about answer = C(N,2) - M? That also uses M and edges. But that is also trivial. However, it's plausible they'd ask such a trivial problem if it's a very easy warm-up? Possibly, but with N up to 1e5 it's still trivial, but they could. However, the problem statement includes the story about the original graph being a tree and edges destroyed, which would be unnecessary if just computing C(N,2)-M. But it provides context. Many simple problems have such stories. Yet, I think the "guaranteed non-edges" interpretation is more interesting and uses the tree property.
Let's search online (in my mind) for a known problem: I recall a problem "Utkarsh and Random Graph" maybe? There is a CodeChef problem "UCL" no.
Another way: The problem could be from Codeforces Round #366 Div2 problem A? Not sure.
Given that the problem appears on a platform (maybe CodeChef), it's more likely to have a non-trivial solution.
Thus I'm comfortable with the deduced solution.
Now, we must write the solution in C++ (or appropriate language). Since the user asked for "provide the final answer", maybe they want the reasoning and the code? The prompt says: "The problem description... Write a solution." So we need to output a solution (code). We need to produce a C++ solution.
We'll implement DSU.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+5;
int parent[MAXN], sz[MAXN];
void make_set(int v) {
parent[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a); b = find_set(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a,b);
parent[b] = a;
sz[a] += sz[b];
}
}
int main() {
int N, M;
cin >> N >> M;
for (int i=1; i<=N; i++) make_set(i);
for (int i=0; i<M; i++) {
int a, b;
cin >> a >> b;
union_sets(a,b);
}
long long ans = 0;
// To avoid double counting, we can iterate over roots
for (int i=1; i<=N; i++) {
if (find_set(i) == i) { // root
long long s = sz[i];
if (s >= 2) {
ans += (s-1)*(s-2)/2;
}
}
}
cout << ans << endl;
return 0;
}
But careful: In DSU, after unions, we might have path compression not applied to all nodes? In the loop we call find_set(i) which compresses path, and then compare with i. But note that after unions, some nodes' parent might not be root if we didn't compress all. However, our find_set will compress and return root, and we compare with i: if i is not root, condition false. So we only count each root once. And we have sz[root] maintained correctly as the component size (since we always updated root sizes during union). This works.
Alternatively, we could avoid find_set for each i by having an array visited, but it's fine.
Time: O(N α(N) + M α(N)) which is fine.
Now, is there any chance that the given edges might not be a forest? As argued, they are subset of tree edges, so acyclic. So DSU will not encounter cycles, but even if there were, it would still work; but we rely on the property that each component has exactly size-1 edges, which holds if the given edges form a forest. If there were cycles, then the given graph would have more edges than a tree, contradicting that they are from a tree. So safe.
Edge Cases: N=1, M=0: loop i=1..1, root itself with s=1, condition s>=2 false, ans=0, output 0.
Now, final answer: Provide explanation and code.
But the user request: "Write a solution." So we need to produce a final answer: maybe just the code? Usually in these tasks, we provide a complete solution with explanation. The instruction says: "You are ChatGPT... Write a solution." So we should produce a final answer that includes the code. Also maybe include a brief explanation. The output format is just the integer.
Thus I'll produce a final message with explanation and code.
Double-check the formula: ans = Σ_i C(s_i - 1, 2). For s_i=1, C(0,2)=0. For s_i=2, C(1,2)=0. For s_i=3, C(2,2)=1. So only components of size at least 3 contribute. Good.
Now, also note that M may be zero, then all components are size 1, ans=0.
Now, let's test with a random case: N=6, M=5 (full tree). Then one component size 6, ans = (5*4)/2=10. C(6,2)-5=15-5=10. Works.
Now, test with N=6, M=4, suppose edges: 1-2,2-3,4-5,5-6 (two components size3 each). ans = 1+1=2. C(6,2)-? Actually original tree has 5 edges. We know 4, missing 1. The missing edge must connect the two components. So the guaranteed non-edges: within each component, the non-edge is the one not directly connected (1-3 and 4-6). So 2. Good.
Thus the solution is solid.
Now, we must consider whether the problem might ask for something else: "how many unordered pairs (A, B) exist such that A and B were not connected via a direct edge in the original graph." Could be interpreted as "pairs that are not directly connected in the original graph" which is constant. But the sample output is 3, which equals that constant for N=4. If we misinterpret, then for N=4, answer always 3 regardless of input. But they gave input with M=3 edges, which happens to be the full tree. If they gave input with M=2, would the answer still be 3? If they expected the constant, they would output 3 regardless, but the problem might have test cases where M < N-1, so output would be 3, but would that be correct under constant interpretation? Yes, because the original graph always had N-1=3 edges, so number of non-edges is C(4,2)-3=3. So answer is always 3 for N=4. So if they had a test case N=4, M=2, they would still expect 3. That is possible. But then why ask for M and edges? Possibly to distract. But in a contest, they'd not ask to read input that is not used. That would be weird but sometimes they do for red herring? Rare.
But given constraints M ≤ N-1, they could have just said N only. But they specifically include M and edges, so they are used.
Thus our interpretation is almost certainly correct.
Now, write final answer.
But also note: The problem might be from some platform where they use 0-based indexing? Usually nodes are 1-indexed.
Thus final solution.
Now, let's produce final answer with code in C++.
We'll include necessary headers and using namespace std.
Potential pitfall: Use long long for answer.
Now, produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int parent[MAXN], sz[MAXN];
void make_set(int v) {
parent[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
for (int i = 1; i <= N; ++i) make_set(i);
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
union_sets(a, b);
}
long long ans = 0;
for (int i = 1; i <= N; ++i) {
if (find_set(i) == i) { // i is the root of its component
long long s = sz[i];
if (s >= 3) {
ans += (s - 1) * (s - 2) / 2; // C(s-1, 2)
}
}
}
cout << ans << endl;
return 0;
}