Тёма

  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
package main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeMap;
public class Loops {
private static class Vertex {
public int n, id, dst;
public Vertex sdom = this, label = this, ancestor = null,
parent = null, dom = null;
public List<Vertex> out, in, bucket;
public Vertex(int count, int n_) {
out = new ArrayList<>(count);
in = new ArrayList<>(count);
bucket = new ArrayList<>(5);
n = n_;
}
}
public static int counter = 0;
public static void dfs(List<Vertex> g) {
dfs_(g.get(0));
}
public static void dfs_(Vertex v) {
v.dst = -2;
v.n = counter++;
for (Vertex u : v.out)
if (u.dst != -2) {
u.parent = v;
dfs_(u);
}
}
public static Vertex findMin(Vertex v) {
if (v.ancestor == null)
return v;
else {
Stack<Vertex> s = new Stack<>();
Vertex u = v;
while (u.ancestor.ancestor != null) {
s.push(u);
u = u.ancestor;
}
while (!s.empty()) {
v = s.pop();
if (v.ancestor.label.sdom.n < v.label.sdom.n)
v.label = v.ancestor.label;
v.ancestor = u.ancestor;
}
return v.label;
}
}
public static void dominators(List<Vertex> g) {
for (int i = g.size() - 1; i > 0; i--) {
Vertex w = g.get(i);
for (Vertex v : w.in) {
Vertex u = findMin(v);
if (u.sdom.n < w.sdom.n)
w.sdom = u.sdom;
}
w.ancestor = w.parent;
w.sdom.bucket.add(w);
for (Vertex v : w.parent.bucket) {
Vertex u = findMin(v);
v.dom = (u.sdom == v.sdom) ? w.parent : u;
}
w.parent.bucket.clear();
}
for (int i = 1; i < g.size(); i++) {
Vertex w = g.get(i);
if (w.dom != w.sdom)
w.dom = w.dom.dom;
}
g.get(0).dom = null;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List<Vertex> g = new ArrayList<>(n);
Map<Integer, Integer> ids = new TreeMap<>();
for (int i = 0; i < n; g.add(new Vertex(2, i++)))
;
in.nextLine();
for (int i = 0; i < n; i++) {
String t[] = in.nextLine().split(" ");
Vertex v = g.get(i);
v.id = Integer.parseInt(t[0]);
ids.put(v.id, i);
if (i + 1 < n && t[1].charAt(0) != 'J') {
v.out.add(g.get(i + 1));
g.get(i + 1).in.add(v);
}
if (t.length == 2)
v.dst = -1;
else
v.dst = Integer.parseInt(t[2]);
}
in.close();
for (Vertex v : g)
if (v.dst != -1) {
v.out.add(g.get(ids.get(v.dst)));
g.get(ids.get(v.dst)).in.add(v);
}
dfs(g);
g.removeIf((v) -> {
return v.dst != -2;
});
for (Vertex u : g)
u.in.removeIf((v) -> {
return v.dst != -2;
});
Collections.sort(g, (a, b) -> {
return a.n - b.n;
});
dominators(g);
int ans = 0;
for (Vertex v : g)
for (Vertex u : v.in) {
for (; u != null && u != v; u = u.dom)
;
if (u == v) {
ans++;
break;
}
}
System.out.println(ans);
}
}