# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1026520 |
2024-07-18T07:27:44 Z |
Whisper |
Zagrade (COI17_zagrade) |
C++17 |
|
754 ms |
56552 KB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (a); ++i)
#define REPD(i, n) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 300005
int numNode;
string s;
vector<int> G[MAX];
int sz[MAX], del[MAX], label[MAX];
void reSubsize(int u, int p = -1){
sz[u] = 1;
for (int &v : G[u]) if(v ^ p){
if (del[v]) continue;
reSubsize(v, u);
sz[u] += sz[v];
}
}
int findCentroid(int u, int p, int siz){
for (int &v : G[u]) if(v ^ p){
if(del[v]) continue;
if (2 * sz[v] > siz) return findCentroid(v, u, siz);
}
return u;
}
map<int, int> cnt;
int ans = 0;
void dfs(int u, int p, int pref, int mxpref, int mnpref, bool add){
pref += label[u]; maximize(mxpref, pref); minimize(mnpref, pref);
if(add){
if (-pref + mxpref <= 0 && pref >= 0){
cnt[pref]++;
}
}
else{
if (-pref + mnpref >= 0){
assert(pref <= 0);
ans += 1LL * cnt[-pref];
}
}
for (int&v : G[u]) if(v ^ p){
if (!del[v]) {
dfs(v, u, pref, mxpref, mnpref, add);
}
}
}
void decompose(int root){
reSubsize(root);
int siz = sz[root];
root = findCentroid(root, root, siz);
cnt.clear();
del[root] = 1;
if(label[root] != -1) cnt[label[root]]++;
for (int&v : G[root]) if(!del[v]){
dfs(v, root, 0, 0, 0, 0);
dfs(v, root, label[root], label[root], label[root], 1);
}
ans += 1LL * cnt[0];
cnt.clear();
reverse(G[root].begin(), G[root].end());
for (int&v : G[root]) if(!del[v]){
dfs(v, root, 0, 0, 0, 0);
dfs(v, root, label[root], label[root], label[root], 1);
}
for (int&v : G[root]) if(!del[v]){
decompose(v);
}
}
void process(void){
cin >> numNode >> s;
for (int i = 1; i <= numNode; ++i) label[i] = (s[i - 1] == '(' ? 1 : -1);
for (int i = 1; i < numNode; ++i){
int u, v; cin >> u >> v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
decompose(1);
cout << ans;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10280 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10136 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
47212 KB |
Output is correct |
2 |
Correct |
512 ms |
50764 KB |
Output is correct |
3 |
Correct |
332 ms |
47248 KB |
Output is correct |
4 |
Correct |
498 ms |
49884 KB |
Output is correct |
5 |
Correct |
326 ms |
47080 KB |
Output is correct |
6 |
Correct |
410 ms |
48360 KB |
Output is correct |
7 |
Correct |
330 ms |
47080 KB |
Output is correct |
8 |
Correct |
406 ms |
48360 KB |
Output is correct |
9 |
Correct |
337 ms |
47208 KB |
Output is correct |
10 |
Correct |
703 ms |
56352 KB |
Output is correct |
11 |
Correct |
300 ms |
47080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10280 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10136 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10280 KB |
Output is correct |
11 |
Correct |
347 ms |
47212 KB |
Output is correct |
12 |
Correct |
512 ms |
50764 KB |
Output is correct |
13 |
Correct |
332 ms |
47248 KB |
Output is correct |
14 |
Correct |
498 ms |
49884 KB |
Output is correct |
15 |
Correct |
326 ms |
47080 KB |
Output is correct |
16 |
Correct |
410 ms |
48360 KB |
Output is correct |
17 |
Correct |
330 ms |
47080 KB |
Output is correct |
18 |
Correct |
406 ms |
48360 KB |
Output is correct |
19 |
Correct |
337 ms |
47208 KB |
Output is correct |
20 |
Correct |
703 ms |
56352 KB |
Output is correct |
21 |
Correct |
300 ms |
47080 KB |
Output is correct |
22 |
Correct |
406 ms |
30072 KB |
Output is correct |
23 |
Correct |
430 ms |
29904 KB |
Output is correct |
24 |
Correct |
412 ms |
29924 KB |
Output is correct |
25 |
Correct |
447 ms |
30008 KB |
Output is correct |
26 |
Correct |
392 ms |
36016 KB |
Output is correct |
27 |
Correct |
410 ms |
33256 KB |
Output is correct |
28 |
Correct |
410 ms |
32172 KB |
Output is correct |
29 |
Correct |
729 ms |
56508 KB |
Output is correct |
30 |
Correct |
754 ms |
56552 KB |
Output is correct |
31 |
Correct |
89 ms |
29660 KB |
Output is correct |
32 |
Correct |
286 ms |
47080 KB |
Output is correct |