-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path280.go
68 lines (61 loc) · 1.02 KB
/
280.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
// UVa 280 - Vertex
package main
import (
"fmt"
"io"
"os"
)
var (
matrix [][]bool
visited map[int]bool
)
func dfs(node int) {
for i, vi := range matrix[node] {
if vi && !visited[i] {
visited[i] = true
dfs(i)
}
}
}
func output(out io.Writer, n int, visited map[int]bool) {
fmt.Fprint(out, n-len(visited))
for i := 0; i < n; i++ {
if !visited[i] {
fmt.Fprintf(out, " %d", i+1)
}
}
fmt.Fprintln(out)
}
func main() {
in, _ := os.Open("280.in")
defer in.Close()
out, _ := os.Create("280.out")
defer out.Close()
var n, s, e, q, qs int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
matrix = make([][]bool, n)
for i := range matrix {
matrix[i] = make([]bool, n)
}
for {
if fmt.Fscanf(in, "%d", &s); s == 0 {
break
}
for {
if fmt.Fscanf(in, "%d", &e); e == 0 {
break
}
matrix[s-1][e-1] = true
}
}
for fmt.Fscanf(in, "%d", &q); q > 0; q-- {
fmt.Fscanf(in, "%d", &qs)
visited = make(map[int]bool)
dfs(qs - 1)
output(out, n, visited)
}
}
}