This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
int n,d[maxn],nxt[maxn],f[maxn],val[maxn];
char c[maxn];
void solve(){
cin >> n;
for(int i=1;i<=2*n;i++){
cin >> c[i];
nxt[i]=val[i]=f[i]=0;
}
for(int i=1;i<=n;i++){
int a,b;cin >> a >> b;
if(c[a]==c[b]){
if(c[a]=='(') d[i]=0,val[a]++;
else d[i]=1,val[b]--;
}
else{
nxt[a]=b,f[a]=i;
if(c[a]=='(') d[i]=1,val[b]--;
else d[i]=0,val[a]--;
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
int cnt=0;
for(int i=1;i<=2*n;i++){
if(f[i]) pq.push({nxt[i],f[i]});
cnt+=val[i];
if(cnt<0){
if(pq.empty()) break;
auto [x,id]=pq.top();pq.pop();
d[id]^=1;
if(x<=i) cnt+=2;
else cnt+=1,val[x]++;
}
}
if(cnt) cout << -1 << '\n';
else{
for(int i=1;i<=n;i++) cout << d[i] << ' ';
cout << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test;cin >> test;
while(test--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |