-
Notifications
You must be signed in to change notification settings - Fork 27
/
539.go
57 lines (50 loc) · 883 Bytes
/
539.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
// UVa 539 - The Settlers of Catan
package main
import (
"fmt"
"os"
)
var (
matrix [][]bool
max int
)
func dfs(node, depth int) {
if depth > max {
max = depth
}
for i := range matrix {
if matrix[i][node] {
matrix[i][node], matrix[node][i] = false, false
dfs(i, depth+1)
matrix[i][node], matrix[node][i] = true, true
}
}
}
func backtracking() {
for i := range matrix {
dfs(i, 0)
}
}
func main() {
in, _ := os.Open("539.in")
defer in.Close()
out, _ := os.Create("539.out")
defer out.Close()
var n, m, n1, n2 int
for {
if fmt.Fscanf(in, "%d%d", &n, &m); n == 0 && m == 0 {
break
}
max = 0
matrix = make([][]bool, n)
for i := range matrix {
matrix[i] = make([]bool, n)
}
for ; m > 0; m-- {
fmt.Fscanf(in, "%d%d", &n1, &n2)
matrix[n1][n2], matrix[n2][n1] = true, true
}
backtracking()
fmt.Fprintln(out, max)
}
}