-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10227.go
86 lines (78 loc) · 1.34 KB
/
10227.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// UVa 10227 - Forests
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func unionFind(x int, f []int) int {
if f[x] != x {
f[x] = unionFind(f[x], f)
}
return f[x]
}
func heardSameTrees(t1, t2 map[int]bool) bool {
if len(t1) != len(t2) {
return false
}
for k := range t1 {
if !t2[k] {
return false
}
}
return true
}
func solve(grid []map[int]bool) int {
p := len(grid)
f := make([]int, p)
for i := range f {
f[i] = i
}
for i := 0; i < p-1; i++ {
for j := i + 1; j < p; j++ {
if heardSameTrees(grid[i], grid[j]) {
if fi, fj := unionFind(i, f), unionFind(j, f); fi != fj {
f[fi] = fj
}
}
}
}
var count int
for i := range f {
if i == f[i] {
count++
}
}
return count
}
func main() {
in, _ := os.Open("10227.in")
defer in.Close()
out, _ := os.Create("10227.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var p, t int
var line string
s.Scan()
kase, _ := strconv.Atoi(s.Text())
for s.Scan(); kase > 0 && s.Scan(); kase-- {
fmt.Sscanf(s.Text(), "%d%d", &p, &t)
grid := make([]map[int]bool, p)
for i := range grid {
grid[i] = make(map[int]bool)
}
for s.Scan() {
if line = s.Text(); line == "" {
break
}
fmt.Sscanf(line, "%d%d", &p, &t)
grid[p-1][t] = true
}
fmt.Fprintln(out, solve(grid))
if kase > 1 {
fmt.Fprintln(out)
}
}
}