DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Fracture Ray

Back to problem

Editorial

Removing an edge set FFF disconnects the graph if and only if FFF contains a nonempty cut. Since ∣F∣=3|F|=3∣F∣=3, only cuts of sizes 111, 222, and 333 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 h(e)h(e)h(e) to every tree edge as well.

The XOR of h(e)h(e)h(e) 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 2−1282^{-128}2−128. Therefore, deleting three edges e1,e2,e3e_1,e_2,e_3e1​ disconnects the graph exactly when one of the following tests succeeds, except with the stated negligible error probability:

  • Some h(ei)h(e_i)h(ei​) is zero.
  • Two fingerprints are equal.
  • h(e1)xorh(e2)xorh(e3)=0h(e_1)\mathbin{\mathrm{xor}}h(e_2)\mathbin{\mathrm{xor}}h(e_3)=0h(e.

A connected simple graph with three edges is a star, a length-three path, or a triangle.

Stars. Fix the center vvv. The edges from vvv into each connected component of G−vG-vG−v form one group. Removing three incident edges disconnects the graph exactly when the chosen edges contain one complete group.

Let a,b,ca,b,ca,b,c be the numbers of groups of sizes 1,2,31,2,31,2,3, respectively, and let ddd be the degree of vvv. The number of valid stars centered at vv is

(d3)−(d−a3)+b(d−a−2)+c.\binom d3-\binom{d-a}3+b(d-a-2)+c.(3d​)−(3d−a​

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 O(Mlog⁡N)O(M\log N)O(MlogN) time.

Paths and triangles. Fix an edge uvuvuv as the middle edge. Choose another edge incident to uuu and another edge incident to vvv. Every length-three path is counted once, while every triangle is counted three times.

Let the middle fingerprint be bbb and the outer fingerprints be x,yx,yx,y. If xxx or yyy equals 000 or bbb, 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 O(MM)O(M\sqrt M)O(MM​) by orienting edges according to degree. Let QQQ be the number of triangles satisfying the cut test. Since the middle-edge method counts each such triangle three times, the final answer is

(number of stars)+(middle-edge count)−2Q.(\text{number of stars})+(\text{middle-edge count})-2Q.(number of stars)+(middle-edge count)−2Q.

The time complexity is O((N+M)log⁡N+MMlog⁡M)O((N+M)\log N+M\sqrt M\log M)O((N+M)logN+MM​ 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

,
e2​
,
e3​
1
​
)
xor
h(e2​)xor
h(e3​)=
0
v
)
+
b(d−
a−
2)+
c.
y=xy=xy=x
y=xxorby=x\mathbin{\mathrm{xor}}by=xxorb
O(MM)O(M\sqrt M)O(MM​)
log
M
)
O(N+M)O(N+M)O(N+M)
2−1282^{-128}2−128
NNN
Δ\DeltaΔ
O(N+MΔ2)O(N+M\Delta^2)O(N+MΔ2)