题目链接:传送门
/*
【模拟】【洛谷】【P3952时间复杂度】
很多细节,找到合适的方法,真的就会容易很多很多
*/
#include<iostream>
#include<stack>
#include<map>
#define int long long
using namespace std;
signed main()
{
int t;cin>>t;
while(t--)
{
int n;string str;
int time=0;
cin>>n>>str;
if(str[2]=='1')
time=0;
else{
int i=4;
while(isdigit(str[i]))
time=time*10+(str[i++]-'0');
}//求出 时间复杂度 0 表示 O(1) time 表示O(n^time);
//初始化
stack<char> st;
map<char,int> mp;
int ans=0;//实际程序的O(n^ans)
int flag=0;//表示程序没有错误! flag 最高优先级 表示 ERR
int as[200],ast=-1;//用来 表示每层循环是否能取到
while(n--)
{
char ch;cin>>ch;
if(ch=='F')
{
ast++; //层数 1表示该层为 常数复杂度 2表示该层为0(n)复杂度 0表示假循环 后面的循环都无效
string s1,s2;
cin>>ch>>s1>>s2;
//特判 ch是否重复 若有 程序错误 输出ERR
st.push(ch);
if(mp[ch]==1) flag=1;
else mp[ch]=1;
//判断两个字符串
int cotA=0,cotB=0,i=0;
while(isdigit(s1[i]))
cotA=cotA*10+(s1[i++]-'0');
i=0;
while(isdigit(s2[i]))
cotB=cotB*10+(s2[i++]-'0');
/*
n n
76 12
12 12
12 n
n 12
12 n 正常循环 O(n) cotB==0&&cotA!=0 标识 2
n 12 76 12 假循环 cotA==0&&cotB!=0 cotA>cotB 标识0 作用见ch=='E'
n n 12 12 复杂度O(1) cotA<=cotB 即可 标识 1
*/
if(cotA!=0&&cotB==0) as[ast]=2;
else if(cotA==0&&cotB!=0||cotA>cotB) as[ast]=0;
else if(cotA<=cotB) as[ast]=1;
}
else{//ch=='E'
int cot=0;
for(int i=0;i<=ast;i++)
{
if(as[i]==2) cot++;
if(as[i]==0) break;//表示该层为假循环 后面的不考虑
}
ans=max(ans,cot);//维护 最大值
ast--;
if(st.empty()) flag=1;
else{
mp[st.top()]=0;
st.pop();
}
}
}
//cout<<ans<<"--"<<time<<" "<<st.size()<<endl;
if(!st.empty()||flag) cout<<"ERR\n";
else if(ans==time) cout<<"Yes\n";
else cout<<"No\n";
}
}