-
Notifications
You must be signed in to change notification settings - Fork 26
/
315.go
82 lines (74 loc) · 1.32 KB
/
315.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
// UVa 315 - Network
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
matrix [][]bool
visited map[int]bool
)
func dfs(curr int) {
visited[curr] = true
for i := range matrix[curr] {
if matrix[curr][i] && !visited[i] {
dfs(i)
}
}
}
func solve() int {
cnt := 0
for i := range matrix {
var turnOff []int
for j := range matrix[i] {
if matrix[i][j] {
turnOff = append(turnOff, j)
matrix[i][j], matrix[j][i] = false, false
}
}
visited = make(map[int]bool)
dfs((i + 1) % len(matrix))
if len(visited)+1 != len(matrix) {
cnt++
}
for _, j := range turnOff {
matrix[i][j], matrix[j][i] = true, true
}
}
return cnt
}
func main() {
in, _ := os.Open("315.in")
defer in.Close()
out, _ := os.Create("315.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var n, c1, c2 int
var line string
for s.Scan() {
if fmt.Sscanf(s.Text(), "%d", &n); n == 0 {
break
}
matrix = make([][]bool, n)
for i := range matrix {
matrix[i] = make([]bool, n)
}
for s.Scan() {
if line = s.Text(); line == "0" {
break
}
r := strings.NewReader(line)
fmt.Fscanf(r, "%d", &c1)
for {
if _, err := fmt.Fscanf(r, "%d", &c2); err != nil {
break
}
matrix[c1-1][c2-1], matrix[c2-1][c1-1] = true, true
}
}
fmt.Fprintln(out, solve())
}
}