DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

Edge Smuggling

Back to problem

Editorial

Let HHH be the undirected graph after adding the hidden edge. Since GGG is connected, a path already exists between the two endpoints of the added edge. Hence the added edge lies on a cycle of HHH and belongs to one nontrivial biconnected component.

A nontrivial component here means a vertex-biconnected block other than a one-edge bridge block. The edges are partitioned into these blocks.

We use an st-ordering. Given a biconnected graph and one of its edges (s,t)(s,t)(s,t), an ordering with the following properties exists and can be found in linear time:

  • sss is first and ttt is last.
  • Every vertex other than s,ts,ts,t has at least one earlier neighbor and at least one later neighbor.

In the first execution, compute the biconnected components of HHH.

For the block containing the added edge, choose its endpoints as s,ts,ts,t, compute an st-ordering, and orient every edge from the earlier endpoint to the later endpoint. Inside this block, only sss has internal indegree 000, and only ttt has internal outdegree 000. The edge from ss to is exactly the added edge.

For every other nontrivial block, choose an arbitrary edge (s,t)(s,t)(s,t). Compute an st-ordering, orient every other edge from the earlier endpoint to the later endpoint, and reverse only the chosen edge to point from ttt to sss. Every internal vertex already has both an incoming and an outgoing edge by the st-ordering property. The reversed edge gives sss an incoming edge and gives ttt an outgoing edge. Thus every vertex of this block has positive internal indegree and positive internal outdegree.

Bridge edges may be oriented arbitrarily. In the second execution, biconnected components can be recomputed after ignoring directions, so one-edge bridge blocks are simply ignored.

In the second execution, compute indegrees and outdegrees using only the edges inside each nontrivial block. Exactly one block has one vertex sss of internal indegree 000 and one vertex ttt of internal outdegree 000. The edge s→ts\to ts→t in this block is the added edge.

Permuting vertex and edge labels preserves biconnected components and all internal degree properties, so the edge is always recoverable.

Tarjan's algorithm and a linear-time st-ordering algorithm give O(N+M)O(N+M)O(N+M) time and O(N+M)O(N+M)O(N+M) memory in each execution.

s
ttt