#include<bits/stdc++.h>
using namespace std;
int n, l[100002], r[100002];
string z;
int ans[100002];
int gr[200002];
set<pair<int, int>> st;
signed main(){
ios_base::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
function<void()> test_case = [](){
cin >> n >> z;
st.clear();
z = '.' + z;
for(register int i = 1; i <= n; ++i){
cin >> l[i] >> r[i];
gr[l[i]] = r[i];
gr[r[i]] = l[i];
if(z[l[i]] == '(' && z[r[i]] == '('){
ans[i] = 0;
z[r[i]] = '.';
}else if(z[l[i]] == ')' && z[r[i]] == ')'){
ans[i] = 1;
z[l[i]] = '.';
}else if(z[l[i]] == '(' && z[r[i]] == ')'){
st.insert({l[i], i});
z[l[i]] = '.';
ans[i] = 1;
}else{ // ) (
st.insert({r[i], i});
z[r[i]] = '.';
ans[i] = 0;
}
}
if(n & 1) return cout << -1 << '\n', void();
int tot(0);
bool flag = true;
for(register int i = 1; i <= 2*n && flag; ++i){
if(z[i] ^ '.') tot += (z[i]=='('?1:-1);
while(tot < 0 && !st.empty()){
int pos = st.begin()->first, j = st.begin()->second;
if(pos <= i){
z[pos] = '(', ++tot;
z[gr[pos]] = '.';
if(gr[pos] <= i) ++tot;
ans[j] ^= 1;
st.erase(st.begin());
}else{
// flag = false;
break;
}
}
}
while(tot < 0 && !st.empty()){
int pos = st.begin()->first, j = st.begin()->second;
z[pos] = '(', ++tot;
z[gr[pos]] = '.';
++tot;
ans[j] ^= 1;
st.erase(st.begin());
}
if(tot) return cout << -1 << '\n', void();
function<bool(const string)> is_valid = [](const string &s){
int tot(0);
for(register int i = 0; i < (int)s.size(); ++i){
tot += (s[i]=='('?1:-1);
if(tot < 0) return false;
}
return !tot;
};
string tmp = "";
for(register int i = 1; i <= 2*n; ++i){
if(z[i] == '.') continue;
tmp += z[i];
}
if(is_valid(tmp)){
for(register int i = 1; i <= n; ++i) cout << ans[i] << ' ';
}else{
cout << -1;
}
cout << '\n';
};
while(T--) test_case();
return 0;
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:23:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
23 | for(register int i = 1; i <= n; ++i){
| ^
Main.cpp:52:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
52 | for(register int i = 1; i <= 2*n && flag; ++i){
| ^
Main.cpp: In lambda function:
Main.cpp:106:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
106 | for(register int i = 0; i < (int)s.size(); ++i){
| ^
Main.cpp: In lambda function:
Main.cpp:116:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
116 | for(register int i = 1; i <= 2*n; ++i){
| ^
Main.cpp:122:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
122 | for(register int i = 1; i <= n; ++i) cout << ans[i] << ' ';
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
724 KB |
Output is correct |
3 |
Correct |
14 ms |
1116 KB |
Output is correct |
4 |
Correct |
13 ms |
1116 KB |
Output is correct |
5 |
Correct |
11 ms |
1368 KB |
Output is correct |
6 |
Correct |
7 ms |
1620 KB |
Output is correct |
7 |
Correct |
9 ms |
1880 KB |
Output is correct |
8 |
Correct |
11 ms |
1964 KB |
Output is correct |
9 |
Correct |
14 ms |
2120 KB |
Output is correct |
10 |
Correct |
14 ms |
2360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
17 ms |
508 KB |
Output is correct |
3 |
Correct |
19 ms |
1008 KB |
Output is correct |
4 |
Correct |
17 ms |
1420 KB |
Output is correct |
5 |
Correct |
21 ms |
1884 KB |
Output is correct |
6 |
Correct |
20 ms |
2392 KB |
Output is correct |
7 |
Correct |
13 ms |
2648 KB |
Output is correct |
8 |
Correct |
16 ms |
3200 KB |
Output is correct |
9 |
Correct |
14 ms |
3420 KB |
Output is correct |
10 |
Correct |
21 ms |
4168 KB |
Output is correct |
11 |
Correct |
25 ms |
4664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |