// in the name of God
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fast() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mk make_pair
#define ppb pop_back
#define endl '\n'
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define file() freopen("input.txt", "r", stdin);
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7, maxn = 2e3 + 10, sq = 3163, inf = 1e18 + 10, lg = 25, pp = 4447, modd = 1e9 + 9;
int n, ans; string s;
vector<vector<int>> g(maxn);
void dfs(int u, int root, int par = -1, string ss = ""){
ss += s[u];
if(ss.size() % 2 == 0){
int x = 0, y = 0;
for(int i = 0; i < ss.size(); ++i){
if(ss[i] == '(') x++;
else y++;
if(y > x) return;
}
if(x == y) ans++;
}
for(int v : g[u]){
if(v != par) dfs(v, root, u, ss);
}
}
signed main(){
fast();//file();
cin >> n >> s;
for(int i = 1; i < n; ++i){
int u, v; cin >> u >> v;
u--; v--;
g[v].pb(u);
g[u].pb(v);
}
for(int i = 0; i < n; ++i){
dfs(i, i);
}
cout << ans;
}
// Running from the daylight...
Compilation message
zagrade.cpp: In function 'void dfs(long long int, long long int, long long int, std::string)':
zagrade.cpp:30:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0; i < ss.size(); ++i){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
348 KB |
Output is correct |
2 |
Correct |
25 ms |
552 KB |
Output is correct |
3 |
Correct |
16 ms |
348 KB |
Output is correct |
4 |
Correct |
10 ms |
348 KB |
Output is correct |
5 |
Correct |
12 ms |
548 KB |
Output is correct |
6 |
Correct |
23 ms |
348 KB |
Output is correct |
7 |
Correct |
21 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
14 ms |
556 KB |
Output is correct |
10 |
Correct |
7 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1520 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
348 KB |
Output is correct |
2 |
Correct |
25 ms |
552 KB |
Output is correct |
3 |
Correct |
16 ms |
348 KB |
Output is correct |
4 |
Correct |
10 ms |
348 KB |
Output is correct |
5 |
Correct |
12 ms |
548 KB |
Output is correct |
6 |
Correct |
23 ms |
348 KB |
Output is correct |
7 |
Correct |
21 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
14 ms |
556 KB |
Output is correct |
10 |
Correct |
7 ms |
348 KB |
Output is correct |
11 |
Runtime error |
2 ms |
1520 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |