Submission #207085

#TimeUsernameProblemLanguageResultExecution timeMemory
207085HideoZagrade (COI17_zagrade)C++11
0 / 100
3082 ms39204 KiB
#pragma GCC optimize ("Ofast") #pragma GCC target ("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; #define all(s) s.begin(), s.end() #define ok puts("ok") #define ll long long #define pb push_back #define mk make_pair #define fr first #define sc second #define vi vector < int > #define pi pair < int, int > #define pii pair < int, pi > #define next next123 const int N = 3e5 + 7; const int INF = 1e9 + 7; int d[N], sub[N], st[N], fin[N]; int n; ll ans; string s; vi g[N]; void dfs (int v, int p = 0){ sub[v] = 1; for (int to : g[v]){ if (!d[to] && to != p){ dfs(to, v); sub[v] += sub[to]; } } } int fc (int v){ bool good = true; int sz = sub[v], p = 0; while (good){ good = false; for (int to : g[v]){ if (!d[to] && to != p && sub[to] * 2 >= sz){ good = true; p = v; v = to; break; } } } return v; } vi nw; void calc (int v, int p, int ad = 0, int bal = 0, int mnb = 0, int mxb = 0){ if (s[v] == '(') bal++; else bal--; mxb = max(mxb, bal); mnb = min(mnb, bal); if (mxb == bal){ if (bal){ nw.pb(bal); if (mxb == bal) ans += fin[bal + ad - (!ad)]; } else { nw.pb(INF); if (mxb == bal && ad) ans += fin[bal - 1]; } } if (mnb == bal){ if (bal){ nw.pb(bal); if (mnb == bal) ans += st[bal + (!ad) - ad]; } else { nw.pb(-INF); if (mnb == bal && !ad) ans += st[bal - 1]; } } for (int to : g[v]) if (!d[to] && to != p) calc(to, v, ad, bal, mnb, mxb); } void cd (int v = 1){ dfs(v); v = fc(v); memset(fin, 0, sizeof(fin)); memset(st, 0, sizeof(st)); fin[0] = st[0] = 1; for (int to : g[v]){ if (!d[to]){ nw.clear(); calc(to, v, (s[v] == '(')); for (int it : nw){ if (it == -INF) fin[0]++; else if (it == INF) st[0]++; else if (it < 0) fin[-it]++; else st[it]++; } } } d[v] = 1; for (int to : g[v]) if (!d[to]) cd(to); } main(){ cin >> n >> s; s = ' ' + s; for (int i = 1; i < n; i++){ int a, b; scanf("%d%d", &a, &b); g[a].pb(b); g[b].pb(a); } cd(); cout << ans; }

Compilation message (stderr)

zagrade.cpp:121:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
zagrade.cpp: In function 'int main()':
zagrade.cpp:126:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...