Asakura's Chase

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 G G . The starting positions on the graph, K ( y o n ) K_{(yon)} and A ( s a k u r a ) A_{(sakura)} will be decided by the players at the start. A A chooses her starting desk, followed by K K . Starting with A A and alternating each round, the active player may choose to move from a desk to any connected desk on G G . G G is assumed to be reflexive--that is to say, a player may choose to stay at a desk instead of moving. Assuming that K K plays optimally, is there a finite series of moves that allows A A to catch K K ?

Input

The first line will contain two integers, N N and M M , the number of nodes and edges respectively. M M lines will follow.

Each following line will contain two integers, u , v u, v , indicating an edge u v uv on G G .

1 N 1000 1 \le N \le 1000

1 M N ( N + 1 ) / 2 1 \le M \le N(N+1)/2

1 u , v N 1 \le u, v \le N

Output

If a finite series of moves exists that allow Asakura to catch up to Kyon ( A = K A = K under optimal play for K K ), print

Kyon is doomed!

Otherwise, print

Safe!

Sample Input 1

6 6
1 2
2 3
3 4
4 5
5 6
6 1

Sample Input 2

7 7
1 3
2 3
3 4
4 7
7 5
5 6
6 7

Output for Sample Input 1

Safe!

Output for Sample Input 2

Kyon is doomed!

Explanation for Sample Input 1

Here is what the classroom looks like: img img 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.

Explanation for Sample Input 2

Here is what the classroom looks like: img img

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.


The answer is 6.

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.

1 solution

Spencer Whitehead
Jan 30, 2016

This is an example of the cop-win problem. Essentially, for all graphs G G in the test data, find if G 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 ) 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:

1. A "fold" of a graph requires the same number of cops to police it as the base graph
2. A fold may be formed by choosing a vertex u covered by another vertex v and removing it (Where covering means that the neighbours of u are a subset of the neighbours of v unison v)
3. By recursively folding G, you will end up with an empty graph (cop-win) or a non-empty graph (robber-win)

We can implement this algorithm efficiently in O ( n 3 ) O(n^3) time by use of this theorem. Implementation for a single case is shown below. Case separation is then simply input parsing.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <cmath>
#include <string.h>
#include <queue>

using namespace std;

int matrix[1005][1005];
int intree[1005], inqueue[1005];
int N, M;

bool cover(int u, int v) {
  for (int i = 0; i < N; i++) {
    if (intree[i]) {
      if (matrix[u][i] == 1 && matrix[v][i] == 0)
        return false;
    }
  }
  return true;
}

bool copwin() {
  queue<int> q;
  int treesize = N;
  for (int i = 0; i < N; i++) {
    intree[i] = 1;
    inqueue[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i != j && !inqueue[i] && matrix[i][j] && cover(i, j)) {
        q.push(i);
        inqueue[i] = true;
        break;
      }
    }
  }
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    intree[c] = false;
    inqueue[c] = false;
    treesize--;
    for (int i = 0; i < N; i++) {
      if (matrix[c][i]) {
        if (intree[i] && !inqueue[i]) {
          for (int j = 0; j < N; j++) {
            if (i != j && matrix[i][j] && cover(i, j)) {
              q.push(i);
              inqueue[i] = true;
              break;
            }
          }
        }
      }
    }
  }
  return (treesize <= 0);
}

int main() {
  memset(matrix, 0, sizeof matrix);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    matrix[i][i] = 1;
  }
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    matrix[u-1][v-1] = matrix[v-1][u-1] = 1;
  }
  cout << (copwin() ? "Kyon is doomed!" : "Safe!") << endl;
}

An alternate solution is to check for instances of C k , k 4 C_k, k \ge 4 in G G , as any k-cycle of length 4 or greater cannot be folded.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...