-
Notifications
You must be signed in to change notification settings - Fork 26
/
10158.go
67 lines (61 loc) · 1.02 KB
/
10158.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
63
64
65
66
67
// UVa 10158 - War
package main
import (
"fmt"
"io"
"os"
)
func unionFind(x int, f []int) int {
if f[x] != x {
f[x] = unionFind(f[x], f)
}
return f[x]
}
func do(out io.Writer, n, c, x, y int, f []int) {
fx1, fx2 := unionFind(x, f), unionFind(x+n, f)
fy1, fy2 := unionFind(y, f), unionFind(y+n, f)
switch c {
case 1:
if fx1 == fy2 {
fmt.Fprintln(out, -1)
} else {
f[fx1], f[fx2] = fy1, fy2
}
case 2:
if fx1 == fy1 {
fmt.Fprintln(out, -1)
} else {
f[fx1], f[fx2] = fy2, fy1
}
case 3:
if fx1 == fy1 {
fmt.Fprintln(out, 1)
} else {
fmt.Fprintln(out, 0)
}
case 4:
if fx1 == fy2 {
fmt.Fprintln(out, 1)
} else {
fmt.Fprintln(out, 0)
}
}
}
func main() {
in, _ := os.Open("10158.in")
defer in.Close()
out, _ := os.Create("10158.out")
defer out.Close()
var n, c, x, y int
fmt.Fscanf(in, "%d", &n)
f := make([]int, 2*n)
for i := range f {
f[i] = i
}
for {
if fmt.Fscanf(in, "%d%d%d", &c, &x, &y); c == 0 && x == 0 && y == 0 {
break
}
do(out, n, c, x, y, f)
}
}