Submission #1081487

# Submission time Handle Problem Language Result Execution time Memory
1081487 2024-08-30T05:44:19 Z juicy Zagrade (COI17_zagrade) C++17
100 / 100
467 ms 55956 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
 
const int N = 3e5 + 5;
 
int n;
int sz[N], x[N], y[N], f[N], b[N], mx[N], mn[N], dep[N];
bool del[N];
long long res;
string s;
vector<int> g[N];
 
void dfs(int u, int p) {
  sz[u] = 1;
  for (int v : g[u]) {
    if (!del[v] && v != p) {
      dfs(v, u);
      sz[u] += sz[v];
    }
  }
}
 
int findCen(int u, int p, int tot) {
  for (int v : g[u]) {
    if (!del[v] && v != p && sz[v] * 2 > tot) {
      return findCen(v, u, tot);
    }
  }
  return u;
}
 
vector<int> ver;
 
void DFS(int u, int p) {
  x[u] = x[p] + (s[u] == '(' ? 1 : -1);
  y[u] = y[p] + (s[u] == '(' ? 1 : -1);
  mx[u] = max(mx[p], x[u]);
  mn[u] = min(mn[p], y[u]);
  dep[u] = dep[p] + 1;
  ver.push_back(u);
  for (int v : g[u]) {
    if (!del[v] && v != p) {
      DFS(v, u);
    }
  }
}
 
void cd(int u) {
  dfs(u, u);
  u = findCen(u, u, sz[u]);
  del[u] = 1;
  dep[u] = 1;
  ++b[0];
  f[1] += s[u] == '(';
  mn[u] = mx[u] = y[u] = 0;
  x[u] = s[u] == '(' ? 1 : -1;
  mx[u] = max(mx[u], x[u]);
  int d = 1;
  for (int v : g[u]) {
    if (!del[v]) {
      vector<int>().swap(ver);
      DFS(v, u);
      for (int c : ver) {
        d = max(d, dep[c]);
        if (y[c] <= mn[c]) {
          res += f[-y[c]];
        }
        if (x[c] >= mx[c]) {
          res += b[x[c]];
        }
      }
      for (int c : ver) {
        if (y[c] <= mn[c]) {
          ++b[-y[c]];
        }
        if (x[c] >= mx[c]) {
          ++f[x[c]];
        }
      }
    }
  }
  for (int i = 0; i <= d; ++i) {
    f[i] = b[i] = 0;
  }
  for (int v : g[u]) {
    if (!del[v]) {
      cd(v);
    }
  }
}
 
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
 
  cin >> n >> s;
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v; --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  cd(0);
  cout << res;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 5 ms 7520 KB Output is correct
4 Correct 6 ms 7516 KB Output is correct
5 Correct 3 ms 7528 KB Output is correct
6 Correct 4 ms 7516 KB Output is correct
7 Correct 3 ms 7520 KB Output is correct
8 Correct 5 ms 7548 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 52660 KB Output is correct
2 Correct 271 ms 54580 KB Output is correct
3 Correct 255 ms 55956 KB Output is correct
4 Correct 286 ms 54860 KB Output is correct
5 Correct 261 ms 54932 KB Output is correct
6 Correct 248 ms 54916 KB Output is correct
7 Correct 286 ms 54932 KB Output is correct
8 Correct 247 ms 54788 KB Output is correct
9 Correct 313 ms 54868 KB Output is correct
10 Correct 278 ms 54820 KB Output is correct
11 Correct 237 ms 54928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 5 ms 7520 KB Output is correct
4 Correct 6 ms 7516 KB Output is correct
5 Correct 3 ms 7528 KB Output is correct
6 Correct 4 ms 7516 KB Output is correct
7 Correct 3 ms 7520 KB Output is correct
8 Correct 5 ms 7548 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
11 Correct 243 ms 52660 KB Output is correct
12 Correct 271 ms 54580 KB Output is correct
13 Correct 255 ms 55956 KB Output is correct
14 Correct 286 ms 54860 KB Output is correct
15 Correct 261 ms 54932 KB Output is correct
16 Correct 248 ms 54916 KB Output is correct
17 Correct 286 ms 54932 KB Output is correct
18 Correct 247 ms 54788 KB Output is correct
19 Correct 313 ms 54868 KB Output is correct
20 Correct 278 ms 54820 KB Output is correct
21 Correct 237 ms 54928 KB Output is correct
22 Correct 467 ms 30180 KB Output is correct
23 Correct 397 ms 29952 KB Output is correct
24 Correct 353 ms 29476 KB Output is correct
25 Correct 357 ms 30136 KB Output is correct
26 Correct 258 ms 38012 KB Output is correct
27 Correct 240 ms 34876 KB Output is correct
28 Correct 241 ms 33172 KB Output is correct
29 Correct 242 ms 54928 KB Output is correct
30 Correct 200 ms 54860 KB Output is correct
31 Correct 73 ms 28616 KB Output is correct
32 Correct 217 ms 54760 KB Output is correct