Submission #969186

#TimeUsernameProblemLanguageResultExecution timeMemory
969186Beerus13Zagrade (COI17_zagrade)C++14
100 / 100
713 ms111356 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long const int N = 3e5 + 5; int n, a[N], sz[N], cnt[N]; bool del[N]; vector<int> g[N], tmp; ll ans, cen; int dfs_size(int u, int p = 0) { sz[u] = 1; for(int v : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u); return sz[u]; } int find_centroid(int u, int p, int num) { for(int v : g[u]) if(v != p && !del[v] && sz[v] > num / 2) return find_centroid(v, u, num); return u; } void dfs_open(int u, int p = 0, int val = 0, int num_close = 0) { num_close = max(num_close, 0); if(a[u] == -1) ++num_close; else --num_close; val += a[u]; if(num_close <= 0) { ans += cnt[val]; } for(int v : g[u]) if(v != p && !del[v]) { dfs_open(v, u, val, num_close); } } void dfs_close(int add, int u, int p = 0, int val = 0, int num_open = 0) { num_open = max(num_open, 0); if(a[u] == 1) { ++num_open; --val; } else { --num_open; ++val; } if(num_open <= 0) { cnt[val] += add; tmp.push_back(val); } for(int v : g[u]) if(v != p && !del[v]) { dfs_close(add, v, u, val, num_open); } } void decomposition(int u) { u = find_centroid(u, 0, dfs_size(u)); cen = u; del[u] = 1; dfs_close(1, u); ans += cnt[0]; for(int v : g[u]) if(!del[v]) { dfs_close(-1, v, u, -a[u], a[u]); dfs_open(v, u); dfs_close(1, v, u, -a[u], a[u]); } dfs_close(-1, u); for(int v : g[u]) if(!del[v]) { decomposition(v); } } void solve() { cin >> n; string s; cin >> s; for(int i = 0; i < s.size(); ++i) { a[i + 1] = s[i] == '(' ? 1 : -1; } for(int i = 1, u, v; i < n; ++i) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } decomposition(1); cout << ans << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; // cin >> test; while(test--) solve(); return 0; } // https://oj.uz/problem/view/COI17_zagrade

Compilation message (stderr)

zagrade.cpp: In function 'void solve()':
zagrade.cpp:74:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for(int i = 0; i < s.size(); ++i) {
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...