# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
894927 |
2023-12-29T08:48:04 Z |
vjudge1 |
Zagrade (COI17_zagrade) |
C++11 |
|
145 ms |
36100 KB |
#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 <ll> q[N];
bool arr[N];
ll ans, n;
void over(){
ll cur = 0, plus = 0;
map<ll, ll> mp;
for(ll 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;
map<ll, ll> mp1;
for(ll 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(ll v, ll p, ll 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(ll 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(ll i = 0; i < n; i++){
if(s[i] == '(') arr[i + 1] = true;
}
for(ll 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
5 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
4 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7512 KB |
Output is correct |
9 |
Correct |
5 ms |
7512 KB |
Output is correct |
10 |
Correct |
4 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
145 ms |
36100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
5 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
4 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7512 KB |
Output is correct |
9 |
Correct |
5 ms |
7512 KB |
Output is correct |
10 |
Correct |
4 ms |
7512 KB |
Output is correct |
11 |
Incorrect |
145 ms |
36100 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |