#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse4,abm,avx,popcnt")
#include<bits/stdc++.h>
#define int long long
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define F first
#define S second
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define lowbit(x) (x&-x)
using namespace std;
typedef pair<int,int> pii;
void debug(){
cout<<"\n";
}
template<class T,class ...U>
void debug(T a,U ...b){
cout<<a<<" ",debug(b...);
}
const int N=2e5+7;
const int INF=1e18L;
pii p[N];
int to[N];
int dp[N];
int ch[N];
void solve(){
int n;
cin>>n;
fill(to,to+(n<<1),-1);
fill(dp,dp+(n<<1),0LL);
string s;
cin>>s;
set<pii> st;
int cnt=0;
int szv=0;
rep(i,0,n-1){
cin>>p[i].F>>p[i].S;
--p[i].F,--p[i].S;
if(s[p[i].F]!=s[p[i].S]) {
szv++;
if(s[p[i].F]==')') dp[p[i].F]--,ch[i]=0;
else {
dp[p[i].S]--;
ch[i]=1;
}
to[p[i].F]=i;
}
else{
if(s[p[i].F]=='(') dp[p[i].F]++,cnt++,ch[i]=0;
else {
dp[p[i].S]--;
ch[i]=1;
cnt--;
}
}
}
int total=-cnt+szv;
if(total%2){
cout<<"-1\n";
return ;
}
int a=total/2;
int b=a+cnt;
if(a<0||b<0) {
cout<<"-1\n";
return ;
}
int sum=0;
priority_queue<pii,vector<pii>,greater<pii> > pq;
rep(i,0,(n<<1)-1){
if(i) dp[i]+=dp[i-1];
if(to[i]!=-1) pq.push(mp(p[to[i]].S,to[i]));
//cout<<dp[i]<<" \n"[i==n*2-1];
while(dp[i]<0&&a){
if(!sz(pq)) {
cout<<"-1\n";
return ;
}
a--;
pii k=pq.top();
//debug("use",k.F,k.S,"swich",p[k.S].F,p[k.S].S);
dp[i]+=1;
dp[max(k.F,i)]+=1;
ch[k.S]^=1;
pq.pop();
}
if(dp[i]<0) {
cout<<"-1\n";
return ;
}
//cout<<dp[i]<<" \n"[i==2*n-1];
}
rep(i,0,n-1){
cout<<ch[i]<<" \n"[i==n-1];
}
}
signed main(){
quick
int t;
cin>>t;
while(t--){
solve();
}
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:73:9: warning: unused variable 'sum' [-Wunused-variable]
73 | int sum=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
6748 KB |
Output is correct |
2 |
Correct |
13 ms |
6488 KB |
Output is correct |
3 |
Correct |
19 ms |
7000 KB |
Output is correct |
4 |
Correct |
23 ms |
6748 KB |
Output is correct |
5 |
Correct |
19 ms |
6680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
6492 KB |
Output is correct |
2 |
Correct |
15 ms |
6760 KB |
Output is correct |
3 |
Correct |
11 ms |
6748 KB |
Output is correct |
4 |
Correct |
15 ms |
6912 KB |
Output is correct |
5 |
Correct |
11 ms |
6928 KB |
Output is correct |
6 |
Correct |
7 ms |
6844 KB |
Output is correct |
7 |
Correct |
9 ms |
7024 KB |
Output is correct |
8 |
Correct |
10 ms |
6888 KB |
Output is correct |
9 |
Correct |
12 ms |
6888 KB |
Output is correct |
10 |
Correct |
14 ms |
6888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
15 ms |
6488 KB |
Output is correct |
3 |
Correct |
16 ms |
6748 KB |
Output is correct |
4 |
Correct |
15 ms |
7128 KB |
Output is correct |
5 |
Correct |
17 ms |
7228 KB |
Output is correct |
6 |
Correct |
18 ms |
7544 KB |
Output is correct |
7 |
Correct |
7 ms |
6748 KB |
Output is correct |
8 |
Correct |
14 ms |
7448 KB |
Output is correct |
9 |
Correct |
12 ms |
7524 KB |
Output is correct |
10 |
Correct |
18 ms |
7776 KB |
Output is correct |
11 |
Correct |
20 ms |
7504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
6748 KB |
Output is correct |
2 |
Correct |
13 ms |
6488 KB |
Output is correct |
3 |
Correct |
19 ms |
7000 KB |
Output is correct |
4 |
Correct |
23 ms |
6748 KB |
Output is correct |
5 |
Correct |
19 ms |
6680 KB |
Output is correct |
6 |
Correct |
11 ms |
6492 KB |
Output is correct |
7 |
Correct |
15 ms |
6760 KB |
Output is correct |
8 |
Correct |
11 ms |
6748 KB |
Output is correct |
9 |
Correct |
15 ms |
6912 KB |
Output is correct |
10 |
Correct |
11 ms |
6928 KB |
Output is correct |
11 |
Correct |
7 ms |
6844 KB |
Output is correct |
12 |
Correct |
9 ms |
7024 KB |
Output is correct |
13 |
Correct |
10 ms |
6888 KB |
Output is correct |
14 |
Correct |
12 ms |
6888 KB |
Output is correct |
15 |
Correct |
14 ms |
6888 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
15 ms |
6488 KB |
Output is correct |
18 |
Correct |
16 ms |
6748 KB |
Output is correct |
19 |
Correct |
15 ms |
7128 KB |
Output is correct |
20 |
Correct |
17 ms |
7228 KB |
Output is correct |
21 |
Correct |
18 ms |
7544 KB |
Output is correct |
22 |
Correct |
7 ms |
6748 KB |
Output is correct |
23 |
Correct |
14 ms |
7448 KB |
Output is correct |
24 |
Correct |
12 ms |
7524 KB |
Output is correct |
25 |
Correct |
18 ms |
7776 KB |
Output is correct |
26 |
Correct |
20 ms |
7504 KB |
Output is correct |
27 |
Correct |
24 ms |
6800 KB |
Output is correct |
28 |
Correct |
21 ms |
6904 KB |
Output is correct |
29 |
Correct |
20 ms |
7172 KB |
Output is correct |
30 |
Correct |
21 ms |
7476 KB |
Output is correct |
31 |
Correct |
19 ms |
7500 KB |
Output is correct |
32 |
Correct |
12 ms |
7452 KB |
Output is correct |
33 |
Correct |
15 ms |
7480 KB |
Output is correct |
34 |
Correct |
22 ms |
7524 KB |
Output is correct |
35 |
Correct |
20 ms |
7524 KB |
Output is correct |
36 |
Correct |
23 ms |
7524 KB |
Output is correct |