-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.cpp
86 lines (73 loc) · 1.51 KB
/
C.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector<vector<ll>> Mat;
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
int n;
string s;
int l[int(3e5 + 5)], r[int(3e5 + 5)];
int dp[int(3e5 + 5)];
bool visited[int(3e5 + 5)];
int dfs(int node) {
if (visited[node]) {
return dp[node];
}
visited[node] = true;
if (l[node] == 0 && r[node] == 0) {
return dp[node] = 0;
}
int ans = 1e9;
if (l[node] != 0) {
if (s[node - 1] == 'L') {
ans = min(ans, dfs(l[node]));
} else {
ans = min(ans, dfs(l[node]) + 1);
}
}
if (r[node] != 0) {
if (s[node - 1] == 'R') {
ans = min(ans, dfs(r[node]));
} else {
ans = min(ans, dfs(r[node]) + 1);
}
}
return dp[node] = ans;
}
int solve() {
cin >> n;
cin >> s;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
}
return dfs(1);
}
void reset() {
fill(dp + 1, dp + n + 1, 0);
fill(visited + 1, visited + n + 1, false);
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
cout << solve() << endl;
reset();
}
return 0;
}