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
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+' '+String.valueOf(bu.key);
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=bu.num;
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=bu.num;
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;
}
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);
}
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);
}
}
}
}