Submission #761622

# Submission time Handle Problem Language Result Execution time Memory
761622 2023-06-20T05:47:02 Z gun_gan Zagrade (COI17_zagrade) C++17
100 / 100
241 ms 124560 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
const int MX = 3e5 + 5, inf = 1e9;
 
int N;
string s;
vector<int> g[MX];
 
int par[MX], bal[MX];
set<pair<int, int>> path[MX][2]; // open, close
// (balance, count)
 
int find(int v) {
      return par[v] == v ? v : par[v] = find(par[v]);
}
 
ll ans = 0;
 
void dfs(int v, int p) {
      for(auto u : g[v]) {
            if(u == p) continue;
            // build 
            bal[u] = bal[v] + (s[u] == '(' ? 1 : -1);
            dfs(u, v);
      }
 
      for(auto u : g[v]) {
            if(u == p) continue; 
 
            // erase invalid
            if(s[v] == ')') {
                  int pu = find(u);
                  auto it = path[pu][0].lower_bound({bal[v], -inf});

                  if(it != path[pu][0].end() && it->first == bal[v]) {
                  
                        path[pu][0].erase(it);
                  }
            }
      
            if(s[v] == '(') {
                  int pu = find(u);

                  auto it = path[pu][1].lower_bound({bal[v], -inf});
                  if(it != path[pu][1].end() && it->first == bal[v]) {
                        path[pu][1].erase(it);
                  } 
            }
      }
 
      // straight path
      if(s[v] == ')') {
            for(auto u : g[v]) {
                  if(u == p) continue;
 
                  int pu = find(u);
                  auto it = path[pu][0].lower_bound({bal[p], -inf});
                  if(it != path[pu][0].end() && it->first == bal[p]) {
                        ans += it->second;
                  }
            }
      } else {
            for(auto u : g[v]) {
                  if(u == p) continue;
 
                  int pu = find(u);
                  auto it = path[pu][1].lower_bound({bal[p], -inf});
                  if(it != path[pu][1].end() && it->first == bal[p]) {
                        ans += it->second;
                  }
            }
      }
 
      // calc answer, merging
      for(auto u : g[v]) {
            if(u == p) continue;
            int pu = find(u), pv = find(v);
 
 
            if((path[pu][0].size() + path[pu][1].size()) > (path[pv][0].size() + path[pv][1].size())) {
                  swap(pu, pv);
            }
 
            for(auto [b, c] : path[pu][0]) {
                  int k = b - bal[p];
                  k = -k + bal[v];
                  auto it = path[pv][1].lower_bound({k, -inf});
                  if(it != path[pv][1].end() && it->first == k) {
                        ans += 1LL * c * it->second;
                  }
            }
 
            for(auto [b, c] : path[pu][1]) {
                  int k = b - bal[p];
                  k = -k + bal[v];
                  auto it = path[pv][0].lower_bound({k, -inf});
                  if(it != path[pv][0].end() && it->first == k) {
                        ans += 1LL * c * it->second;
                  }
            }
 
 
            par[pu] = pv;
            for(auto x : path[pu][0]) {
                  auto it = path[pv][0].lower_bound({x.first, -inf});

                  if(it != path[pv][0].end() && it->first == x.first) {
                        path[pv][0].insert({x.first, x.second + it->second});
                        path[pv][0].erase(it);
                  } else {
                        path[pv][0].insert(x);
                  }
            }

             for(auto x : path[pu][1]) {
                  auto it = path[pv][1].lower_bound({x.first, -inf});
                  if(it != path[pv][1].end() && it->first == x.first) {
                        path[pv][1].insert({x.first, x.second + it->second});
                        path[pv][1].erase(it);
                  } else {
                        path[pv][1].insert(x);
                  }
            }

            path[pu][0].clear();
            path[pu][1].clear();
      }

      // add new path
      if(s[v] == '(') {
            int pv = find(v);
            auto it = path[pv][0].lower_bound({bal[v], -inf});

            if(it != path[pv][0].end() && it->first == bal[v]) {
                  path[pv][0].insert({it->first, it->second + 1});
                  path[pv][0].erase(*it);
            } else {
                  path[pv][0].insert({bal[v], 1});
            }
      } else {
            int pv = find(v);
            auto it = path[pv][1].lower_bound({bal[v], -inf});

            if(it != path[pv][1].end() && it->first == bal[v]) {
                  path[pv][1].insert({it->first, it->second + 1});
                  path[pv][1].erase(*it);
            } else {
                  path[pv][1].insert({bal[v], 1});
            }
      }
}     
 
int main() {
      ios_base::sync_with_stdio(0); cin.tie(0);
 
      cin >> N >> s;
      s = '#' + s;
 
      for(int i = 0; i < N - 1; i++) {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
      }
 
      for(int i = 1; i <= N; i++) par[i] = i;
 
      bal[1] = (s[1] == '(' ? 1 : -1);
      dfs(1, 0);
 
      cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 35540 KB Output is correct
2 Correct 16 ms 35616 KB Output is correct
3 Correct 17 ms 35508 KB Output is correct
4 Correct 17 ms 35540 KB Output is correct
5 Correct 17 ms 35564 KB Output is correct
6 Correct 16 ms 35544 KB Output is correct
7 Correct 16 ms 35608 KB Output is correct
8 Correct 17 ms 35596 KB Output is correct
9 Correct 16 ms 35540 KB Output is correct
10 Correct 16 ms 35540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 113244 KB Output is correct
2 Correct 158 ms 118824 KB Output is correct
3 Correct 156 ms 113384 KB Output is correct
4 Correct 155 ms 117492 KB Output is correct
5 Correct 149 ms 113408 KB Output is correct
6 Correct 152 ms 115288 KB Output is correct
7 Correct 151 ms 113296 KB Output is correct
8 Correct 169 ms 115276 KB Output is correct
9 Correct 147 ms 113364 KB Output is correct
10 Correct 190 ms 120360 KB Output is correct
11 Correct 124 ms 113312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 35540 KB Output is correct
2 Correct 16 ms 35616 KB Output is correct
3 Correct 17 ms 35508 KB Output is correct
4 Correct 17 ms 35540 KB Output is correct
5 Correct 17 ms 35564 KB Output is correct
6 Correct 16 ms 35544 KB Output is correct
7 Correct 16 ms 35608 KB Output is correct
8 Correct 17 ms 35596 KB Output is correct
9 Correct 16 ms 35540 KB Output is correct
10 Correct 16 ms 35540 KB Output is correct
11 Correct 149 ms 113244 KB Output is correct
12 Correct 158 ms 118824 KB Output is correct
13 Correct 156 ms 113384 KB Output is correct
14 Correct 155 ms 117492 KB Output is correct
15 Correct 149 ms 113408 KB Output is correct
16 Correct 152 ms 115288 KB Output is correct
17 Correct 151 ms 113296 KB Output is correct
18 Correct 169 ms 115276 KB Output is correct
19 Correct 147 ms 113364 KB Output is correct
20 Correct 190 ms 120360 KB Output is correct
21 Correct 124 ms 113312 KB Output is correct
22 Correct 241 ms 51856 KB Output is correct
23 Correct 223 ms 51916 KB Output is correct
24 Correct 221 ms 51896 KB Output is correct
25 Correct 234 ms 51896 KB Output is correct
26 Correct 159 ms 77316 KB Output is correct
27 Correct 168 ms 66616 KB Output is correct
28 Correct 170 ms 62296 KB Output is correct
29 Correct 193 ms 124468 KB Output is correct
30 Correct 189 ms 124560 KB Output is correct
31 Correct 93 ms 58632 KB Output is correct
32 Correct 124 ms 117392 KB Output is correct