-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCppp.cxx
93 lines (78 loc) · 1.49 KB
/
Cppp.cxx
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
#include <bits/stdc++.h>
#define large 1000000007
#define BITS 32
#define For(t, i, n) for (t i = 0; i < n; i++)
#define Forr(t, i, a, b) for (t i = a; i <= b; i++)
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;
typedef pair<int, int> pI;
typedef pair<ll, ll> pLL;
struct Node
{
char move;
int left;
int right;
};
int DFS(Node nodes[], int root)
{
Node node = nodes[root];
if (node.left == 0)
{
if (node.right == 0)
return 0;
int op = DFS(nodes, node.right);
if (node.move != 'R')
return 1 + op;
else
return op;
}
if (node.right == 0)
{
int op = DFS(nodes, node.left);
if (node.move != 'L')
return 1 + op;
else
return op;
}
int opl = DFS(nodes, node.left), opr = DFS(nodes, node.right);
if (node.move != 'L')
opl++;
if (node.move != 'R')
opr++;
return min(opl, opr);
}
void test_case()
{
int n;
cin >> n;
string s;
cin >> s;
Node nodes[n + 1];
For(int, i, n)
{
nodes[i + 1].move = s[i];
}
For(int, i, n)
{
int u, v;
cin >> u >> v;
nodes[i + 1].left = u;
nodes[i + 1].right = v;
}
cout << DFS(nodes, 1) << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
// t = 1;
while (t--)
{
test_case();
}
return 0;
}