Submission #545277

#TimeUsernameProblemLanguageResultExecution timeMemory
545277tranxuanbachZagrade (COI17_zagrade)C++17
100 / 100
1493 ms86164 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define endl '\n' #define fi first #define se second #define For(i, l, r) for (auto i = l; i < r; i++) #define ForE(i, l, r) for (auto i = l; i <= r; i++) #define FordE(i, l, r) for (auto i = l; i >= r; i--) #define Fora(v, a) for (auto v: a) #define bend(a) a.begin(), a.end() #define isz(a) ((signed)a.size()) using ll = long long; using ld = long double; using pii = pair <int, int>; using vi = vector <int>; using vpii = vector <pair <int, int>>; using vvi = vector <vector <int>>; template <class T> using ordered_multiset = tree <T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 3e5 + 5; int n; string s; int val[N]; vi adj[N]; int par[N], sz[N]; int ctall; vi adjct[N]; int parct[N], hct[N]; bool block[N]; void dfs_cpn_ct(int u){ sz[u] = 1; Fora(v, adj[u]){ if (v == par[u] or block[v]){ continue; } par[v] = u; dfs_cpn_ct(v); sz[u] += sz[v]; } } int dfs_ct(int u){ par[u] = 0; dfs_cpn_ct(u); if (sz[u] == 1){ return u; } int sz_cpn = sz[u]; while (1){ int v = 0; Fora(tv, adj[u]){ if (tv == par[u] or block[tv]){ continue; } if (sz[tv] * 2 > sz_cpn){ v = tv; break; } } if (v == 0){ break; } u = v; } block[u] = 1; Fora(v, adj[u]){ if (block[v]){ continue; } v = dfs_ct(v); adjct[u].emplace_back(v); parct[v] = u; } return u; } ll ans; void dfs_cpn_bracket_count(int u, int sum, int mnprefup, int mnprefdown, ordered_multiset <int> &mtsup, ordered_multiset <int> &mtsdown){ if (sum >= 0 and mnprefup >= 0){ ans += mtsdown.order_of_key(sum + 1) - mtsdown.order_of_key(sum); } if (sum <= 0 and mnprefdown == sum){ ans += mtsup.order_of_key(-sum + 1) - mtsup.order_of_key(-sum); } Fora(v, adj[u]){ if (v == par[u] or block[v]){ continue; } par[v] = u; dfs_cpn_bracket_count(v, sum + val[v], min(val[v], mnprefup + val[v]), min(mnprefdown, sum + val[v]), mtsup, mtsdown); } } void dfs_cpn_bracket_insert(int u, int sum, int mnprefup, int mnprefdown, ordered_multiset <int> &mtsup, ordered_multiset <int> &mtsdown){ if (sum >= 0 and mnprefup >= 0){ mtsup.insert(sum); } if (sum <= 0 and mnprefdown == sum){ mtsdown.insert(-sum); } Fora(v, adj[u]){ if (v == par[u] or block[v]){ continue; } par[v] = u; dfs_cpn_bracket_insert(v, sum + val[v], min(val[v], mnprefup + val[v]), min(mnprefdown, sum + val[v]), mtsup, mtsdown); } } void dfs_bracket(int u){ ordered_multiset <int> mtsup, mtsdown; if (val[u] == 1){ mtsup.insert(1); } else{ mtsdown.insert(1); } par[u] = 0; Fora(v, adj[u]){ if (block[v]){ continue; } par[v] = u; dfs_cpn_bracket_count(v, val[v], min(val[v], 0), min(val[v], 0), mtsup, mtsdown); dfs_cpn_bracket_insert(v, val[u] + val[v], min({0, val[v], val[v] + val[u]}), min({0, val[u], val[u] + val[v]}), mtsup, mtsdown); } block[u] = 1; Fora(v, adjct[u]){ dfs_bracket(v); } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("KEK.inp", "r", stdin); // freopen("KEK.out", "w", stdout); cin >> n; cin >> s; s = ' ' + s; For(i, 1, n){ int u, v; cin >> u >> v; adj[u].emplace_back(v); adj[v].emplace_back(u); } ForE(i, 1, n){ val[i] = (s[i] == '(' ? 1 : -1); } ctall = dfs_ct(1); memset(block, 0, sizeof(block)); dfs_bracket(ctall); cout << ans << endl; } /* ==================================================+ INPUT: | --------------------------------------------------| --------------------------------------------------| ==================================================+ OUTPUT: | --------------------------------------------------| --------------------------------------------------| ==================================================+ */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...