-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva280.java
More file actions
70 lines (66 loc) · 2.1 KB
/
Uva280.java
File metadata and controls
70 lines (66 loc) · 2.1 KB
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
import java.util.*;
import java.io.*;
public class Uva280
{
static ArrayList<Integer> [] adjList ;
static boolean [] marked ;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
String line ;
while( !(line = br.readLine()).equals("0") ){
Integer n = Integer.parseInt(line);
adjList = new ArrayList[n+1];
for (int i = 0; i < n + 1; i++) {
adjList[i] = new ArrayList<>();
}
String s ;
while( !(s = br.readLine()).equals("0") ){
StringTokenizer st = new StringTokenizer(s);
int u = Integer.parseInt(st.nextToken());
while( st.hasMoreTokens() ){
int v = Integer.parseInt(st.nextToken());
if( v == 0 ){
continue ;
}
adjList[u].add(v);
}
}
StringTokenizer st = new StringTokenizer(br.readLine());
int q = Integer.parseInt(st.nextToken());
for (int i = 0; i < q; i++) {
int x = Integer.parseInt(st.nextToken());
marked = new boolean[n+1];
int sum = 0 ;
for(int y : adjList[x]){
if(!marked[y]){
sum += dfs(y);
}
}
sb.append(n-sum);
for(int j = 1 ; j <= n ; j++){
if(!marked[j]){
sb.append(" ").append(j);
}
}
sb.append('\n');
}
}
pr.print(sb.toString());
pr.close();
br.close();
}
static int dfs(int u)
{
marked[u] = true ;
int sum = 1 ;
for(int v : adjList[u]){
if( !marked[v] ){
sum += dfs(v);
}
}
return sum ;
}
}