#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 3 * 1e5 + 10;
const ll mod = 1e9 + 7;
ll um(ll a, ll b){
return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll add(ll a, ll b){
return (1LL * a + b) % mod;
}
ll binpow(ll x, ll step){
ll res = 1LL;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
vector <int> q[N];
bool arr[N];
int ans, n;
void over(){
int cur = 0, cnt = 1, plus = 0;
map<int, int> mp;
for(int i = 1; i <= n; i++){
if(arr[i]){
plus++;
if(plus == 1) mp[cur + plus] = 1 + mp[cur];
else mp[cur + plus]++;
} else{
plus = 0;
cur++;
ans += mp[cur];
}
}
cur = plus = 0;
cnt = 1;
map<int, int> mp1;
for(int i = n; i >= 1; i--){
if(arr[i]){
plus++;
if(plus == 1) mp1[cur + plus] = 1 + mp1[cur];
else mp1[cur + plus]++;
} else{
plus = 0;
cur++;
ans += mp1[cur];
}
}
}
void dfs(int v, int p, int cnt){
if(cnt == 0) ans++;
for(auto u : q[v]){
if(u == p) continue;
if(arr[u]) dfs(u, v, cnt + 1);
else{
if(cnt > 0) dfs(u, v, cnt - 1);
}
}
}
void subtask1(){
for(int i = 1; i <= n; i++){
if(arr[i] == true) dfs(i, -1, 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> n >> s;
for(int i = 0; i < n; i++){
if(s[i] == '(') arr[i + 1] = true;
}
for(int i = 0, a, b; i < n - 1; i++){
cin >> a >> b;
q[a].pb(b);
q[b].pb(a);
}
if(n <= 1000){
subtask1();
} else over();
cout << ans;
return 0;
}
Compilation message
zagrade.cpp: In function 'void over()':
zagrade.cpp:36:15: warning: variable 'cnt' set but not used [-Wunused-but-set-variable]
36 | int cur = 0, cnt = 1, plus = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7512 KB |
Output is correct |
2 |
Correct |
5 ms |
7516 KB |
Output is correct |
3 |
Correct |
5 ms |
7664 KB |
Output is correct |
4 |
Correct |
3 ms |
7768 KB |
Output is correct |
5 |
Correct |
5 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7664 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
4 ms |
7648 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
133 ms |
31468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7512 KB |
Output is correct |
2 |
Correct |
5 ms |
7516 KB |
Output is correct |
3 |
Correct |
5 ms |
7664 KB |
Output is correct |
4 |
Correct |
3 ms |
7768 KB |
Output is correct |
5 |
Correct |
5 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7664 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
4 ms |
7648 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Incorrect |
133 ms |
31468 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |