import java util class point int op -1 num key String name int time in

  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
import java.util.*;
class point{
int op=-1,num,key;
String name;
int time = 0;
int mark = 0;
point sdom, label, ancestor, parent, dom;
ArrayList<point> in=new ArrayList<point>();
ArrayList<point> out=new ArrayList<point>();
ArrayList<point> bucket=new ArrayList<point>();
point() {
this.sdom = this.label = this;
this.ancestor = this.dom = this.parent = null;
this.in = new ArrayList<point>();
this.out = new ArrayList<point>();
this.bucket = new ArrayList<point>();
}
point(String name, int op, int num, int key){
this.name = name;
this.op = op;
this.num = num;
this.key = key;
}
point(String name, int num, int key){
this.name = name;
this.num = num;
this.key = key;
}
}
public class Factorial {
public static void main(String[] args){
HashMap<Integer, point> Map = new HashMap<Integer, point>();
Scanner sc = new Scanner(System.in);
int i,j,n,ans,k=0,p=0,t=1;
char[]bufff = new char[20];
String buff=new String();
String buff1=new String();
String buff2=new String();
buff1="";
int buf;
buff=sc.nextLine();
n=Integer.valueOf(buff);
point [] graph=new point[n];
point [] forms=new point[n];
for(i=0;i<n;i++){
graph[i]=new point();
forms[i]=new point();
}
point bu=new point();
for(i=0;i<n;i++){
buff=sc.nextLine();
p=0;
bu.op=-1;
for(j=0;j<buff.length();j++){
if(buff.charAt(j)!=' '){
while(j<buff.length() && buff.charAt(j)!=' ')
bufff[k++]=buff.charAt(j++);
}
if(p==0){
for(int z=0;z<k;z++)
buff1+=bufff[z];
bu.key=Integer.valueOf(buff1);
buff1="";
p=1;
k=0;
}
else if(p==1){
for(int z=0;z<k;z++)
buff1+=bufff[z];
bu.name=buff1;
buff1="";
p=2;
k=0;
}
else if(p==2){
for(int z=0;z<k;z++)
buff1+=bufff[z];
bu.op=Integer.valueOf(buff1);
buff1="";
k=0;
}
}
if(bu.name.charAt(0)=='B' ||bu.name.charAt(0)=='J') {
Map.put(bu.key, new point(bu.name, bu.op, i, bu.key));
forms[i].name=bu.name;
forms[i].num=i;
forms[i].key=bu.key;
forms[i].op=bu.op;
}
else {
Map.put(bu.key, new point(bu.name, i, bu.key));
forms[i].name=bu.name;
forms[i].num=i;
forms[i].key=bu.key;
}
if(i+1<n && bu.name.charAt(0)!='J'){
graph[i].out.add(graph[i+1]);
graph[i+1].in.add(graph[i]);
}
graph[i].name=forms[i].name+' '+Integer.toString(forms[i].key);
}
for(i=0;i<n;i++){
if(forms[i].name.charAt(0)!='A'){
graph[forms[i].num].out.add(graph[Map.get(Map.get(forms[i].key).op).num]);
graph[Map.get(Map.get(forms[i].key).op).num].in.add(graph[forms[i].num]);
}
}
dfs(graph[0],t);
List<point> G=new ArrayList<point>();
for(i=0;i<n;i++){
for(j=0;j<graph[i].in.size();j++){
if(graph[i].in.get(j).mark==0){
graph[i].in.remove(j);
}
}
}
for(i=0;i<n;i++){
if(graph[i].mark==1){
G.add(graph[i]);
}
}
Collections.sort(G, (p1, p2) -> {
return p1.time - p2.time;
});
dominators(G);
int answer = 0;
for (point v : G)
for (point u : v.in) {
for (; u != null && u != v; u = u.dom)
;
if (u == v) {
answer++;
break;
}
}
System.out.println(answer);
}
public static void dfs(point p,int time) {
time+=1;
p.mark=1;
p.time=time;
int i;
for(i = 0; i < p.out.size(); i++){
if(p.out.get(i).mark != 1){
p.out.get(i).parent=p;
dfs(p.out.get(i),time);
}
}
}
public static void dominators(List<point> g) {
for (int i = g.size() - 1; i > 0; i--) {
point w = g.get(i);
for (point v : w.in) {
point u = findMin(v);
if (u.sdom.time < w.sdom.time)
w.sdom = u.sdom;
}
w.ancestor = w.parent;
w.sdom.bucket.add(w);
for (point v : w.parent.bucket) {
point u = findMin(v);
v.dom = (u.sdom == v.sdom) ? w.parent : u;
}
w.parent.bucket.clear();
}
for (int i = 1; i < g.size(); i++) {
point w = g.get(i);
if (w.dom != w.sdom)
w.dom = w.dom.dom;
}
g.get(0).dom = null;
}
public static point findMin(point v) {
if (v.ancestor == null)
return v;
else {
Stack<point> s = new Stack<>();
point u = v;
while (u.ancestor.ancestor != null) {
s.push(u);
u = u.ancestor;
}
while (!s.empty()) {
v = s.pop();
if (v.ancestor.label.sdom.time < v.label.sdom.time)
v.label = v.ancestor.label;
v.ancestor = u.ancestor;
}
return v.label;
}
}
}