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>
#define int long long
#define endl "\n"
using namespace std;
const int MAXN = 1e6;
vector <int> p;
string s;
int n, sum, t, resp[MAXN], v[MAXN], sp[MAXN], ss[MAXN], aux[MAXN];
main()
{
cin >> t;
while(t--)
{
p.clear();
cin >> n >> s;
sum = 0;
p.clear();
for(int i = 0; i < 4 * n; i++) resp[i] = v[i] = sp[i] = ss[i] =aux[i] = 0;
for(int i = 1; i <= n; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
if(s[a] != s[b])
{
p.push_back(i);
if(s[a] == '(') aux[i] = 0;
else aux[i] = 1;
}
else
{
if(s[a] == '(') sum++, v[i] = 1;
else sum--, v[i] = -1;
}
}
int tam = p.size(), f = 0;
for(int i = 0; i < p.size(); i++)
{
int r1 = 0, r2 = 1;
if(sum + tam - i <= 0 && sum - tam + i >= 0)
{
cout << -1 << endl;
f = 1;
break;
}
if(sum - tam + i >= 0)
{
v[p[i]] = -1;
if(aux[p[i]]) swap(r1, r2);
resp[p[i]] = r2;
sum--;
}
else
{
v[p[i]] = 1;
if(aux[p[i]]) swap(r1, r2);
resp[p[i]] = r1;
sum++;
}
}
for(int i = 1; i <= n; i++)
{
sp[i] = sp[i - 1] + v[i];
if(sp[i] < 0 && !f)
{
cout << -1 << endl;
f = 1;
}
}
for(int i = n; i >= 0; i--)
{
ss[i] = ss[i + 1] + v[i];
if(ss[i] > 0 && !f)
{
cout << -1 << endl;
f = 1;
}
}
if(!f)
{
for(int i = 1; i <= n; i++)
{
cout << resp[i];
if(i != n) cout << " ";
}
cout << endl;
}
}
}
Compilation message (stderr)
Main.cpp:9:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:37:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 0; i < p.size(); i++)
| ~~^~~~~~~~~~
# | 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... |