Solune @ 2025-01-10 22:13:15
#include<bits/stdc++.h>
using namespace std;
struct sent{
string v;
bool ownn, jin;
int subn;
};
stack<sent> stk;
stack<string> bian, tmp;
bool _find(string tar, bool erase){
bool f = 0;
stack<string> backup = bian;
while(!backup.empty() && backup.top()!= tar){
backup.pop();
}
if(!backup.empty()) f = 1;
if(erase &&!backup.empty()){
stack<string> new_bian;
while(!bian.empty()){
if(bian.top()!= tar) new_bian.push(bian.top());
else {
bian.pop();
break;
}
bian.pop();
}
while(!new_bian.empty()){
bian.push(new_bian.top());
new_bian.pop();
}
}
return f;
}
int main(){
int t, n;
string s, o;
cin >> t;
while(t--){
while(!stk.empty()) stk.pop();
while(!bian.empty()) bian.pop();
cin >> n >> o;
bool err = 0;
if(n % 2) err = 1;
int shi1 = 0, shi2 = 0;
if(o[2] == 'n'){
for(int i = 4; o[i] >= '0' && o[i] <= '9'; ++i)
shi1 = shi1 * 10 + o[i] - '0';
}
char typ;
string nam, beg, fin;
for(int i = 0; i < n; ++i){
cin >> typ;
if(typ == 'E'){
if(stk.empty()){
err = 1;
continue;
}
string v = stk.top().v;
bool own = stk.top().ownn, jin = stk.top().jin;
int sub = stk.top().subn;
if(stk.size() == 1){
if(stk.top().jin) shi2 = stk.top().subn + stk.top().ownn;
else shi2 = 0;
stk.pop();
}
else{
stk.pop();
if(_find(v, 1)){
sub += own;
if(stk.top().subn < sub && jin){
sent tmp = stk.top();
tmp.subn = sub;
stk.pop();
stk.push(tmp);
}
}
else{
err = 1;
continue;
}
}
}
else{
cin >> nam >> beg >> fin;
if(_find(nam, 0)){
err = 1;
continue;
}
bian.push(nam);
if(beg == "n" && fin!= "n") stk.push({nam, 0, 0, 0});
else if(beg == "n" && fin == "n") stk.push({nam, 0, 1, 0});
else if(beg!= "n" && fin == "n") stk.push({nam, 1, 1, 0});
else stk.push({nam, 0, 1, 0});
}
}
if(err ||!stk.empty()) cout << "ERR\n";
else if(shi1 == shi2) cout << "Yes\n";
else cout << "No\n";
}
return 0;
}