The following problem is adapted from the LCC Placement Challenge, question 9. Details on output formats and test cases for Brilliant are given at the bottom of the challenge.
Ryoko Asakura has used her space-warping powers to turn a classroom into a battlefield. For some reason, things no longer follow the rules of Euclidean geometry; you can walk between two desks and possibly end up at the other end of the room! Kyon has been caught in this pocket dimension, and needs to stall while he waits for help to arrive. If Asakura is moving to catch Kyon, while Kyon runs away, will he be able to evade her indefinitely?
More formally, you are given a graph . The starting positions on the graph, and will be decided by the players at the start. chooses her starting desk, followed by . Starting with and alternating each round, the active player may choose to move from a desk to any connected desk on . is assumed to be reflexive--that is to say, a player may choose to stay at a desk instead of moving. Assuming that plays optimally, is there a finite series of moves that allows to catch ?
The first line will contain two integers, and , the number of nodes and edges respectively. lines will follow.
Each following line will contain two integers, , indicating an edge on .
If a finite series of moves exists that allow Asakura to catch up to Kyon ( under optimal play for ), print
Kyon is doomed!
Otherwise, print
Safe!
6 6
1 2
2 3
3 4
4 5
5 6
6 1
7 7
1 3
2 3
3 4
4 7
7 5
5 6
6 7
Safe!
Kyon is doomed!
Here is what the classroom looks like:
Asakura might choose to start at desk 1. Kyon then chooses any desk other than 6 or 2 (say, 3). On her first turn, Asakura moves to desk 2. Kyon responds by moving to desk 4. In this way, he can continue to move to evade Asakura indefinitely.Here is what the classroom looks like:
Asakura might choose to start at desk 4. Kyon then chooses any desk, say 5. On her first turn, Asakura moves to desk 7. Kyon can choose to stay still or move to desk 6. Either way, Asakura catches him next turn.
For our purposes, we will consider the set of 11 test cases here . Each test case is given consecutively, without any separation from the others, subject to the input format described above. Note that the first two cases are the samples. Your answer will be the number of cases in which Kyon is captured by Asakura.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
This is an example of the cop-win problem. Essentially, for all graphs G in the test data, find if G is cop-win or not. An algorithm for the cop-win problem in general exists in polynomial time, using strong products of graphs. This algorithm (discussed in Bonato and Nowakowski's "A Game of Cops and Robbers on Graphs") runs in O ( n 6 ) , which is far too slow for the limit of 1000 on N.
Instead consider a different algorithm, that uses the cop-win strategy. This theorem states in essence:
We can implement this algorithm efficiently in O ( n 3 ) time by use of this theorem. Implementation for a single case is shown below. Case separation is then simply input parsing.
An alternate solution is to check for instances of C k , k ≥ 4 in G , as any k-cycle of length 4 or greater cannot be folded.