Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created June 4, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/c7f41031efe0429ca3fcb0216d1a46e4 to your computer and use it in GitHub Desktop.
Save jianminchen/c7f41031efe0429ca3fcb0216d1a46e4 to your computer and use it in GitHub Desktop.
A B = 5
B D = 6
C D = 3
E F
F G
|
? D
A B
a - c - b - d
a -> c
a/b = a/c / b/c
a/d = a/b / b/d
a a = 1
a b = ?
a c => a/b b/c
a d => a/b b/d
a is the center point
c - a
b - a
d - a
? a
b c
steps:
(1) build graph (n)
(2) find connected components (n)
(3) for every component: (n)
find a center point x
convert their relationships to ? - x
(4) query: x, y (1)
x and y are not in the same com: return -1
else:
x or y is the center point, return v, 1/v
x/c, y/c -> x/y
A-B-D-C ->
E-F-G
O(3*N) + O(M)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment