题目
#include<bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define eb emplace_back
const int N=1e3+9;
int n,m;
vector<pair<char,int> > s[N];
signed main(){ios;cin>>n>>m;for(int i=1;i<=n;i++){char a;cin>>a;s[i].eb(a,i);}while(m--){int op,x,y;cin>>op>>x;if(op!=3){cin>>y;if(op==1)reverse(s[x].begin(),s[x].end());for(auto it:s[x])s[y].eb(it);s[x].clear();}else {vector<pair<char,int> >now;for(auto &[c,i]:s[x]){if(i==x)now.eb(c,i);else s[i].eb(c,i);}swap(now,s[x]);}}vector<bool> ok(n,true);vector<vector<string> >ans(n+7);!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!for(int i=1;i<=n;i++){vector<string> stk;stk.eb();int cnt=0;for(auto &[c,id]:s[i]){if(c=='('){cnt++;stk.eb();}else if(c==')'){if(cnt<1){ok[i]=false;break;}cnt--;if(stk.back().size()){ans[i].eb(stk.back());}stk.pop_back();}else stk.back()+=c;}if(cnt!=0)ok[i]=false;else {if(stk.back().size())ans[i].eb(stk.back());}}for(int i=1;i<=n;i++){if(ok[i]==false){cout<<"-1\n";}else {cout<<ans[i].size();for(auto it:ans[i]){cout<<' '<<it;}cout<<'\n';}}return 0;
}