#FW0000118. Tính tỉ số theo quan hệ (Evaluate Division)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tính tỉ số theo quan hệ (Evaluate Division)
Evaluate Division
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given equations , where and are variable names and . All equations are consistent and there are no division-by-zero cases. For each query , determine the ratio if it follows from the equations, otherwise print -1. A variable absent from all equations is undefined even for a query of the form .
Input
First line ; next lines , then lines . Variable names have no whitespace. This is the stdin/stdout adaptation of the LeetCode API problem.
Output
Print ratios, one per line, or -1 when unknown. Absolute or relative error up to is accepted.
Subtasks
-
Subtask 1 (40 points): , , . All input conditions remain in force.
-
Subtask 2 (60 points): , , . All input conditions remain in force.
Examples
Input
2 5
a b 2
b c 3
a c
b a
a e
a a
x x
Output
6.0000000000
0.5000000000
-1.0000000000
1.0000000000
-1.0000000000
Explanation
a/c = (a/b)*(b/c)=6; b/a=0.5; unknown variables cannot be evaluated, even in x/x.