-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path793.go
61 lines (55 loc) · 999 Bytes
/
793.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
// UVa 793 - Network Connections
package main
import (
"bufio"
"fmt"
"os"
)
func unionFind(x int, f []int) int {
if f[x] != x {
f[x] = unionFind(f[x], f)
}
return f[x]
}
func main() {
in, _ := os.Open("793.in")
defer in.Close()
out, _ := os.Create("793.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var kase, a, b, num int
var command byte
var line string
s.Scan()
fmt.Sscanf(s.Text(), "%d", &kase)
for s.Scan(); kase > 0 && s.Scan(); kase-- {
fmt.Sscanf(s.Text(), "%d", &num)
f := make([]int, num+1)
for i := range f {
f[i] = i
}
ok, ko := 0, 0
for s.Scan() {
if line = s.Text(); line == "" {
break
}
fmt.Sscanf(line, "%c%d%d", &command, &a, &b)
if fa, fb := unionFind(a, f), unionFind(b, f); command == 'c' {
if fa != fb {
f[fa] = fb
}
} else {
if fa != fb {
ko++
} else {
ok++
}
}
}
fmt.Fprintf(out, "%d,%d\n", ok, ko)
if kase > 1 {
fmt.Fprintln(out)
}
}
}