forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa00280_Vertex.java
79 lines (64 loc) · 1.84 KB
/
UVa00280_Vertex.java
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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 216 (280 - Vertex) */
/* SUBMISSION: 09142217 */
/* SUBMISSION TIME: 2011-08-12 16:01:53 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00280_Vertex {
static int n;
static boolean[][] adjacency;
static boolean[] reachable;
static enum color {WHITE, GRAY, BLACK};
static color[] status;
static void dfs(int s) {
status[s] = color.GRAY;
for (int i = 0; i < n; ++i)
if (adjacency[s][i]) {
reachable[i] = true;
if (status[i] == color.WHITE)
dfs(i);
}
status[s] = color.BLACK;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while ((n = Integer.parseInt(in.readLine())) != 0) {
adjacency = new boolean[n][n];
while (true) {
String line = in.readLine();
StringTokenizer stk = new StringTokenizer(line);
int i = Integer.parseInt(stk.nextToken());
if (i == 0)
break;
int j;
while ((j = Integer.parseInt(stk.nextToken())) != 0)
adjacency[i - 1][j - 1] = true;
}
String line = in.readLine();
StringTokenizer stk = new StringTokenizer(line);
int m = Integer.parseInt(stk.nextToken());
for (int i = 0; i < m; ++i) {
reachable = new boolean[n];
status = new color[n];
for (int j = 0; j < n; ++j)
status[j] = color.WHITE;
int s = Integer.parseInt(stk.nextToken());
dfs(s - 1);
int cnt = 0;
StringBuilder res = new StringBuilder();
for (int k = 0; k < n; ++k)
if (!reachable[k])
++cnt;
res.append(cnt);
for (int k = 0; k < n; ++k)
if (!reachable[k])
res.append(" " + (k + 1));
System.out.println(res);
}
}
in.close();
System.exit(0);
}
}