-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWerewolf.java
127 lines (92 loc) · 2.72 KB
/
Werewolf.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
import java.math.RoundingMode;
public class Werewolf
{
static boolean DEBUG = true;
public static class Node
{
int N;
boolean marked;
Set<Node> parents;
Set<Node> children;
public Node(int N) {
this.N = N;
parents = new HashSet<>();
children = new HashSet<>();
marked = false;
}
}
public static void main(String[] argv) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int inhabitants = Integer.parseInt(getLine(br));
Node[] village = new Node[inhabitants+1];
for (int i = 1; i <= inhabitants; i++)
village[i] = new Node(i);
String input;
while(true) {
input = getLine(br);
if ("BLOOD".equals(input)) {
break;
}
int pos = input.indexOf(" ");
int child = Integer.parseInt(input.substring(0,pos));
int parent = Integer.parseInt(input.substring(pos+1));
village[child].parents.add( village[parent] );
village[parent].children.add( village[child] );
}
while (true) {
input = getLine(br);
if (input == null)
break;
int victim = Integer.parseInt(input);
markParents(village[victim]);
markChildren(village[victim]);
village[victim].marked=true;
}
boolean found = false;
for (int i =1 ; i <= inhabitants; i++) {
if (!village[i].marked) {
System.out.print(i+" ");
found = true;
}
}
if (found)
System.out.println("");
else
System.out.println("0");
}
static void markParents(Node node) {
Stack<Node> stack = new Stack<>();
stack.addAll(node.parents);
while (!stack.empty()) {
Node n = stack.pop();
n.marked = true;
stack.addAll(n.parents);
}
}
static void markChildren(Node node) {
Stack<Node> stack = new Stack<>();
stack.addAll(node.children);
while (!stack.empty()) {
Node n = stack.pop();
n.marked = true;
stack.addAll(n.children);
}
}
public static String getLine(BufferedReader br) {
String s = null;
try {
s = br.readLine();
}
catch (Exception e) {
}
return s;
}
public static void warn(String s) {
if (DEBUG) {
System.out.println(s);
}
}
}