#include <bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int MAXN = 2e5;
string s;
int v[MAXN], ls[MAXN], rs[MAXN], aux[MAXN];
int t, n;
main()
{
cin >> t;
while(t--)
{
int c, f;
c = f = 0;
cin >> n >> s;
for(int i = 0; i < 2 * n; i++) v[i] = ls[i] = rs[i] = aux[i] = 0;
for(int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
if(s[a] != s[b])
{
if(s[a] == ')') aux[i]++;
continue;
}
if(s[a] == '(') v[i] = 1;
else v[i] = -1;
}
if(n % 2 == 1) {cout << "-1" << endl; f = 1;}
for(int i = 0; i < n; i++)
{
if(i != 0) ls[i] = ls[i - 1] + v[i];
else ls[i] = v[i];
if(!v[i]) c++;
if(c + ls[i] < 0 && !f)
{
cout << "-1" << endl;
f = 1;
break;
}
}
c = 0;
for(int i = n - 1; i >= 0; i--)
{
if(i != n - 1) rs[i] = rs[i + 1] - v[i];
else rs[i] = -v[i];
if(!v[i]) c++;
if(c + rs[i] < 0 && !f)
{
cout << "-1" << endl;
f = 1;
break;
}
}
int sum = 0;
if(!f)
{
for(int i = 0; i < n; i++)
{
if(v[i]) cout << 0;
else
{
int r1 = 0, r2 = 1;
if(aux[i]) swap(r1, r2);
if(ls[i] - rs[i] + sum <= 0) {cout << r1; sum++;}
else {cout << r2; sum--;}
}
if(i != n - 1) cout << " ";
}
cout << endl;
}
}
}
Compilation message
Main.cpp:9:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |