Problem:
Problem F: Name the Crossing

The city of Kyoto is well-known for its Chinese plan: streets are
either North-South or East-West. Some streets are numbered, but most
of them have real names.

Crossings are named after the two streets crossing there,
e.g. Kawaramachi-Sanjo is the crossing of Kawaramachi street and Sanjo
street. But there is a problem: which name should come first?
At first the order seems quite arbitrary: one says Kawaramachi-Sanjo
(North-South first) but Shijo-Kawaramachi (East-West first). With some
experience, one realizes that actually there
seems to be an "order" on the streets, for instance in the above
Shijo is "stronger" than Kawaramachi, which in turn is "stronger" than
Sanjo.
One can use this order to deduce the names of other crossings.

You are given as input a list of known crossing names X-Y.
Streets are either North-South or East-West, and only orthogonal streets
may cross.

As your list is very incomplete, you start by completing it using
the following rule:

two streets A and B have equal strength if (1) to (3) are
all true:

they both cross the same third street C in the input

there is no street D such that D-A and B-D appear in the input

there is no street E such that A-E and E-B appear in the input

We use this definition to extend our strength relation:

A is stronger than B, when there is a
sequence A = A1, A2, ..., An = B,
with n at least 2,

where, for any i in 1 .. n-1, either
Ai-Ai+1 is an input crossing or
Ai and Ai+1 have equal strength.

Then you are asked whether some other possible crossing names X-Y are
valid. You should answer affirmatively if you can infer the validity of a
name, negatively if you cannot. Concretely:

YES if you can infer that the two streets are orthogonal, and X
is stronger than Y

NO otherwise

Input

The input is a sequence of data sets, each of the form

N
Crossing1
...
CrossingN
M
Question1
...
QuestionM

Both Crossings and Questions are of the form

X-Y

where X and Y are strings of alphanumerical characters,
of lengths no more than 16. There is no white space, and case matters
for alphabetical characters.

N and M are between 1 and 1000 inclusive, and there are
no more than 200 streets in a data set.

The last data set is followed by a line containing a zero.

Output

The output for each data set should be composed of M+1 lines,
the first one containing the number of streets
in the Crossing part of the input, followed by the answers to
each question, either YES or NO without any spaces.

Sample Input

7
Shijo-Kawaramachi
Karasuma-Imadegawa
Kawaramachi-Imadegawa
Nishioji-Shijo
Karasuma-Gojo
Torimaru-Rokujo
Rokujo-Karasuma
6
Shijo-Karasuma
Imadegawa-Nishioji
Nishioji-Gojo
Shijo-Torimaru
Torimaru-Gojo
Shijo-Kawabata
4
1jo-Midosuji
Midosuji-2jo
2jo-Omotesando
Omotesando-1jo
4
Midosuji-1jo
1jo-Midosuji
Midosuji-Omotesando
1jo-1jo
0

Output for the Sample Input

8
YES
NO
YES
NO
YES
NO
4
YES
YES
NO
NO

Solution in Ruby (judge verdict: Accepted)

### constant

MAX_N = 200

D_NS = 0
D_EW = 1

### main

loop do
  n = gets.to_i
  break if n == 0

  nm2id = {}
  nid = 0
  cpairs = []

  n.times do
    st0, st1 = gets.strip.split('-')

    if nm2id[st0].nil?
      nm2id[st0] = nid
      nid += 1
    end
    if nm2id[st1].nil?
      nm2id[st1] = nid
      nid += 1
    end
    cpairs << [nm2id[st0], nm2id[st1]]
  end
  #p nm2id
  #p cpairs

  cmat = nid.times.map{nid.times.map{nil}}
  nbrs = nid.times.map{[]}

  cpairs.each do |st0, st1|
    cmat[st0][st1] = 1
    cmat[st1][st0] = -1
    nbrs[st0] << st1
    nbrs[st1] << st0
  end

  cgrps = []
  ncgrp = 0
  drcs = []

  for id in (0...nid)
    next if drcs[id]

    cgrps[id] = ncgrp
    drcs[id] = D_NS
    q = [id]

    while ! q.empty?
      u = q.shift
      ud = drcs[u]

      for v in nbrs[u]
        if ! drcs[v]
          cgrps[v] = ncgrp
          drcs[v] = 1 - ud
          q << v
        end
      end
    end
    #p drcs

    ncgrp += 1
  end

  dsts = ncgrp.times.map{2.times.map{[]}}
  (0...nid).each{|id| dsts[cgrps[id]][drcs[id]] << id}
  #p dsts

  for i in (0...nid)
    cgrp = cgrps[i]
    d = drcs[i]

    for j in dsts[cgrp][d]
      next if i >= j

      cnd1 = false
      cnd23 = true

      for k in dsts[cgrp][1 - d]
        cik = cmat[i][k]
        cjk = cmat[j][k]
        if cik && cjk
          cnd1 = true
          if cik != cjk
            cnd23 = false
            break
          end
        end
      end

      if cnd1 && cnd23
        cmat[i][j] = cmat[j][i] = 0
        nbrs[i] << j
        nbrs[j] << i
      end
    end
  end
  #p cmat
  #p nbrs

  puts nid

  m = gets.to_i

  m.times do
    q0, q1 = gets.strip.split('-').map{|st| nm2id[st]}

    yorn = false
    if q0 && q1 && drcs[q0] && drcs[q1] && drcs[q0] != drcs[q1]
      visited = nid.times.map{false}
      visited[q0] = true
      q = [q0]

      while ! q.empty?
        u = q.shift
        if u == q1
          yorn = true
          break
        end

        for v in nbrs[u]
          next if visited[v]
          cuv = cmat[u][v]
          if cuv && cuv >= 0
            visited[v] = true
            q << v
          end
        end
      end
    end

    puts (yorn ? "YES" : "NO")
  end
end