Removing an edge set disconnects the graph if and only if contains a nonempty cut. Since , only cuts of sizes , , and matter.
Cut fingerprints. Fix a DFS spanning tree. Give every non-tree edge an independent random 128-bit value and XOR that value at both endpoints. Accumulating the vertex values in postorder assigns a value to every tree edge as well.
The XOR of over every cut is always zero. For a fixed edge set that is not a cut, the probability that its XOR is accidentally zero is at most . Therefore, deleting three edges disconnects the graph exactly when one of the following tests succeeds, except with the stated negligible error probability:
A connected simple graph with three edges is a star, a length-three path, or a triangle.
Stars. Fix the center . The edges from into each connected component of form one group. Removing three incident edges disconnects the graph exactly when the chosen edges contain one complete group.
Let be the numbers of groups of sizes , respectively, and let be the degree of . The number of valid stars centered at is
DFS low-link values identify the separated child subtrees. For every non-tree edge, binary lifting finds the first child below its ancestor endpoint, which allows all group sizes to be computed in time.
Paths and triangles. Fix an edge as the middle edge. Choose another edge incident to and another edge incident to . Every length-three path is counted once, while every triangle is counted three times.
Let the middle fingerprint be and the outer fingerprints be . If or equals or , the cut condition already holds. Otherwise, only and are valid. Maintain fingerprint frequencies at every vertex and iterate over the endpoint of smaller degree. High-degree vertices use hash tables; low-degree vertices use binary search in sorted arrays. The sum of the smaller endpoint degrees over all edges is .
Enumerate triangles in by orienting edges according to degree. Let be the number of triangles satisfying the cut test. Since the middle-edge method counts each such triangle three times, the final answer is
The time complexity is and the space complexity is . Frequency queries at high-degree vertices use expected constant time. The false-positive probability for each fixed non-cut is at most . For small , enumerate all connected triples and use distinct basis vectors for non-tree edges to obtain exact cut fingerprints. With maximum degree , enumerating stars and the two outer edges around each middle edge takes time. The general algorithm above also handles the remaining subtasks.
Solution written by GPT6