-
Notifications
You must be signed in to change notification settings - Fork 27
/
839.go
62 lines (55 loc) · 1023 Bytes
/
839.go
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
// UVa 839 - Not so Mobile
package main
import (
"fmt"
"io"
"os"
)
type mobile struct {
wl, dl, wr, dr int
left, right *mobile
}
func readLine(in io.Reader) *mobile {
var m mobile
fmt.Fscanf(in, "%d%d%d%d", &m.wl, &m.dl, &m.wr, &m.dr)
if m.wl == 0 {
m.left = readLine(in)
}
if m.wr == 0 {
m.right = readLine(in)
}
return &m
}
func solve(root *mobile) (bool, int) {
var ok bool
if root.left != nil {
if ok, root.wl = solve(root.left); !ok {
return false, -1
}
}
if root.right != nil {
if ok, root.wr = solve(root.right); !ok {
return false, -1
}
}
return root.wl*root.dl == root.wr*root.dr, root.wl + root.wr
}
func main() {
in, _ := os.Open("839.in")
defer in.Close()
out, _ := os.Create("839.out")
defer out.Close()
var kase int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanln(in)
root := readLine(in)
if ok, _ := solve(root); ok {
fmt.Fprintln(out, "YES")
} else {
fmt.Fprintln(out, "NO")
}
if kase > 1 {
fmt.Fprintln(out)
}
}
}