# LIGHTSOUT - Editorial

**URL:** <https://discuss.codechef.com/t/lightsout-editorial/126294>\
**Category:** editorial\
**Tags:** easy, editorial, trees, start267\
**Created:** [December 2, 2026, 4:30pm UTC](https://discuss.codechef.com/t/lightsout-editorial/126294 "2026-12-02T16:30:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [December 2, 2026, 4:30pm UTC](https://discuss.codechef.com/t/lightsout-editorial/126294/1 "2026-12-02T16:30:57Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/LIGHTSOUT)  
[Contest: Division 1](https://www.codechef.com/START267A/problems/LIGHTSOUT)  
[Contest: Division 2](https://www.codechef.com/START267B/problems/LIGHTSOUT)  
[Contest: Division 3](https://www.codechef.com/START267C/problems/LIGHTSOUT)  
[Contest: Division 4](https://www.codechef.com/START267D/problems/LIGHTSOUT)

_ **Author:** _ [shadowsama1234](https://www.codechef.com/users/shadowsama1234)  
_ **Tester:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

Easy

# PREREQUISITES:

BFS/DFS

# PROBLEM:

Two crewmates on a tree start at vertices A and B and want to swap positions.  
Each second, each of them either stays or moves to an adjacent vertex, simultaneously.  
They can never be at the same vertex, and can never cross the same edge in opposite directions in the same second.  
Find the minimum time to swap, or -1 if impossible.

# EXPLANATION:

The crewmates need to pass each other somewhere.  
If the tree is a path, that’s clearly impossible, since they can never change their relative order; so the answer is -1.

Otherwise, the tree has at least one vertex with degree \ge 3; let’s call such vertices _junctions_.  
The only way to pass each other is for one crewmate to step into a branch of some junction v, while the other walks through v.

* * *

So, let’s fix the junction v that’s used, and let x = d(A, v) denote the distance (number of edges) on the path from A to v; and similarly y = d(B, v).  
Since v has degree \ge 3, it has a neighbour w that’s on neither the path from v to A nor the path from v to B.

Suppose Crewmate 1 is the one who steps aside to w.  
Then:

- Crewmate 1 needs x+1 seconds to reach w.
- Crewmate 2 can only be at v once Crewmate 1 has left it for w, so they reach v at time T \ge \max(y, x+1).
- Crewmate 2 then needs x more seconds to reach A, finishing at time T + x.
- Crewmate 1 can re-enter v no earlier than time T+1 (by waiting for Crewmate 2 to leave it), and then needs y more seconds to reach B.

Taking T = \max(y, x+1) is obviously achievable: both crewmates walk straight towards v (Crewmate 2 waiting a bit before entering v if needed, and trailing behind Crewmate 1 where their paths merge), and then they follow each other out.  
This gives a total time of \max(T + x, T + 1 + y) with T = \max(y, x+1).

If Crewmate 2 is the one who steps aside instead, the analysis is symmetric.

* * *

The answer is the minimum of this quantity across all junctions v.

To compute the answer for a fixed v, we only need to know distances from v to A and B; or equivalently distances from A and B to v.  
So we can simply run a bfs/dfs from each of A and B to precompute their distances to each vertex, after which each v can be processed in constant time.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> import sys
> input = sys.stdin.readline
> 
> # https://github.com/cheran-senthil/PyRival/blob/master/pyrival/graphs/bfs.py
> def layers(graph, start=0):
> used = [False] * len(graph)
> used[start] = True
> q, ret = [start], []
> while q:
> nq = []
> ret.append(q)
> for v in q:
> for w in graph[v]:
> if not used[w]:
> used[w] = True
> nq.append(w)
> q = nq
> return ret
> 
> def distances(graph, start):
> dist = [0]*len(graph)
> lay = layers(graph, start)
> for d in range(len(lay)):
> for v in lay[d]: dist[v] = d
> return dist
> 
> for _ in range(int(input())):
> n, a, b = map(int, input().split())
> adj = [[] for _ in range(n+1)]
> for i in range(n-1):
> u, v = map(int, input().split())
> adj[u].append(v)
> adj[v].append(u)
>     
> da, db = distances(adj, a), distances(adj, b)
> ans = -1
> for v in range(1, n+1):
> if len(adj[v]) < 3: continue
> for x, y in [(da[v], db[v]), (db[v], da[v])]:
> T = max(y, x+1)
> cur = max(T + x, T + 1 + y)
> if ans == -1 or cur < ans: ans = cur
> print(ans)
> 
> ```
