Влад

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import java.util.*;
class Vertex implements Comparable<Vertex>{
String name = "";
private int time = 0, mark = 0;
private Vertex sdom, label, ancestor, parent, dom;
private ArrayList<Vertex> value, value1, bucket;
Vertex(){
this.sdom = this.label = this;
this.ancestor = this.dom = this.parent = null;
this.value = new ArrayList<Vertex>();
this.value1 = new ArrayList<Vertex>();
this.bucket = new ArrayList<Vertex>();
}
public ArrayList<Vertex> getValue() {
return value;
}
public ArrayList<Vertex> getValue1() {
return value1;
}
public void setMark(int mark) {
this.mark = mark;
}
public void setTime(int time) {
this.time = time;
}
public int getMark() {
return mark;
}
public void setParent(Vertex parent) {
this.parent = parent;
}
@Override
public int compareTo(Vertex o) {
return this.time - o.time;
}
public ArrayList<Vertex> getBucket() {
return bucket;
}
public Vertex getDom() {
return dom;
}
public Vertex getSdom() {
return sdom;
}
public int getTime() {
return time;
}
public Vertex getAncestor() {
return ancestor;
}
public Vertex getLabel() {
return label;
}
public Vertex getParent() {
return parent;
}
public void setAncestor(Vertex ancestor) {
this.ancestor = ancestor;
}
public void setDom(Vertex dom) {
this.dom = dom;
}
public void setLabel(Vertex label) {
this.label = label;
}
public void setSdom(Vertex sdom) {
this.sdom = sdom;
}
}
class Elem{
String command;
int operand = -1, number, key;
Elem(String command, int operand, int number, int key){
this.command = command;
this.operand = operand;
this.number = number;
this.key = key;
}
Elem(String command, int number, int key){
this.command = command;
this.number = number;
this.key = key;
}
}
public class Loops {
private static HashMap<Integer, Elem> help = new HashMap<Integer, Elem>();
private static ArrayList<Elem> all_commands = new ArrayList<Elem>();
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int i, j, n = Integer.valueOf(in.nextLine()), answer;
ArrayList<Vertex> graph = new ArrayList<Vertex>();
for(i = 0; i < n; i++){
graph.add(i, new Vertex());
}
for(i = 0; i < n; i++){
String temp = in.nextLine();
parseString(temp, i, graph);
}
for(i = 0; i < all_commands.size(); i++){
if(all_commands.get(i).operand != -1){
graph.get(all_commands.get(i).number).getValue().add(graph.get(help.get(help.get(all_commands.get(i).key).operand).number));
graph.get(help.get(help.get(all_commands.get(i).key).operand).number).getValue1().add(graph.get(all_commands.get(i).number));
}
}
//System.out.println(graph.size());
dfs(graph.get(0));
Iterator<Vertex> iter = graph.iterator();
while (iter.hasNext()) {
Vertex vertex = iter.next();
if(vertex.getMark() == 0) iter.remove();
}
for(i = 0; i < graph.size(); i++){
iter = graph.get(i).getValue1().iterator();
while (iter.hasNext()) {
Vertex vertex = iter.next();
if (vertex.getMark() == 0) iter.remove();
}
}
Collections.sort(graph);
//System.out.println(graph.size());
dominators(graph);
for(i = answer = 0; i < graph.size(); i++){
for(j = 0; j < graph.get(i).getValue1().size(); j++){
Vertex w = graph.get(i).getValue1().get(j);
while(w != null && w != graph.get(i)){
w = w.getDom();
}
if(w == graph.get(i)) {
answer++;
break;
}
}
}
System.out.println(answer);
}
public static void parseString(String s, int number, ArrayList<Vertex> graph){
int i, flag = 1;
String key = "", operand = "", name = "";
for(i = 0; i < s.length(); i++){
if(s.charAt(i) != ' '){
if(s.charAt(i) == 'A'){
name = "ACTION";
i += 5;
if(number+1 < graph.size()){
graph.get(number).getValue().add(graph.get(number+1));
graph.get(number+1).getValue1().add(graph.get(number));
}
graph.get(number).name = name+' '+key;
}
else if(s.charAt(i) == 'B'){
name = "BRANCH";
flag = 0;
i += 4;
if(number+1 < graph.size()){
graph.get(number).getValue().add(graph.get(number+1));
graph.get(number+1).getValue1().add(graph.get(number));
}
graph.get(number).name = name+' '+key;
}
else if(s.charAt(i) == 'J'){
name = "JUMP";
flag = 0;
i += 3;
graph.get(number).name = name+' '+key;
}
else if(i < s.length() && (int) s.charAt(i) > 47 && (int) s.charAt(i) < 58){
String temp = "";
while(i < s.length() && (int) s.charAt(i) > 47 && (int) s.charAt(i) < 58){
temp += s.charAt(i++);
}
i--;
if(flag == 1){
key = temp;
}
else operand = temp;
}
}
}
if(flag == 1){
help.put(Integer.valueOf(key), new Elem(name, number, Integer.valueOf(key)));
all_commands.add(new Elem(name, number, Integer.valueOf(key)));
}
else{
help.put(Integer.valueOf(key), new Elem(name, Integer.valueOf(operand), number, Integer.valueOf(key)));
all_commands.add(new Elem(name, Integer.valueOf(operand), number, Integer.valueOf(key)));
}
}
private static int time = 1;
public static void dfs(Vertex vertex) {
vertex.setMark(1);
vertex.setTime(time++);
int i;
for(i = 0; i < vertex.getValue().size(); i++){
if(vertex.getValue().get(i).getMark() != 1){
vertex.getValue().get(i).setParent(vertex);
dfs(vertex.getValue().get(i));
}
}
}
public static void dominators(ArrayList<Vertex> graph){
int i, j, k;
for(i = graph.size()-1; i > 0; i--){
Vertex w = graph.get(i);
for(j = 0; j < w.getValue1().size(); j++){
Vertex u = FindMin(graph, w.getValue1().get(j));
if(u.getSdom().getTime() < w.getSdom().getTime()) w.setSdom(u.getSdom());
}
w.setAncestor(w.getParent());
w.getSdom().getBucket().add(w);
for(k = 0; k < w.getParent().getBucket().size(); k++){
Vertex u = FindMin(graph, w.getParent().getBucket().get(k));
if((u.getSdom() == w.getParent().getBucket().get(k).getSdom())){
w.getParent().getBucket().get(k).setDom(w.getParent());
}
else w.getParent().getBucket().get(k).setDom(u);
}
w.getParent().getBucket().clear();
}
for(i = 1; i < graph.size(); i++){
Vertex w = graph.get(i);
if(w.getDom() != w.getSdom()) w.setDom(w.getDom().getDom());
}
graph.get(0).setDom(null);
}
public static Vertex FindMin(ArrayList<Vertex> graph, Vertex v){
if(v.getAncestor() == null) return v;
else{
Stack<Vertex> stack = new Stack<Vertex>();
Vertex u = v;
while (u.getAncestor().getAncestor() != null){
stack.push(u);
u = u.getAncestor();
}
while(stack.size() != 0){
v = stack.pop();
if(v.getAncestor().getLabel().getSdom().getTime() < v.getLabel().getSdom().getTime()) v.setLabel(v.getAncestor().getLabel());
v.setAncestor(u.getAncestor());
}
return v.getLabel();
}
}
}