Submission #931663

# Submission time Handle Problem Language Result Execution time Memory
931663 2024-02-22T08:22:25 Z vjudge1 Zagrade (COI17_zagrade) C++17
10 / 100
192 ms 1480 KB
#include <bits/stdc++.h>
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);
#define what(x) cerr << #x << " is " << x << '\n';
#define kill(x) {cout << x << endl; return 0;}
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define F first
#define S second
const ll inf = 1e18, mod = 1e9 + 7, delta = 1e9 + 9, SQ = 450, P = 6065621;
 
using namespace std;

const int N = 1010, LG = 13;
int n, m, h[N], par[N][LG];
vector<int> adj[N];
string s, t;

bool check (string s) {
  int n = s.size();
  int cnt = 0;
  for (auto u: s) {
    if (u == '(') ++cnt;
    else {
      if (cnt == 0) return false;
      --cnt;
    }
  }
  return cnt == 0;
}

void dfs (int v, int p = -1) {
  par[v][0] = p;
  if (p + 1) h[v] = h[p] + 1;
  for (int i = 1; i < LG; i++)
    if (par[v][i - 1] + 1)
      par[v][i] = par[par[v][i - 1]][i - 1];
  for (auto u: adj[v])
    if (u - p) dfs(u, v);
}

int lca (int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  for (int i = LG - 1; i >= 0; i--) 
    if (par[u][i] + 1 && h[par[u][i]] >= h[v])
      u = par[u][i];
  if (u == v) return v;
  for (int i = LG - 1; i >= 0; i--)
    if (par[u][i] != par[v][i])
      u = par[u][i], v = par[v][i];
  return par[v][0];
}

int main () {
  fast_io;
  cin >> n >> t;
  t = '.' + t;
  memset(par, -1, sizeof par);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dfs(1);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      s = "";
      int l = lca(i, j);
      if (l == i) {
        for (int f = j; f != i; f = par[f][0])
          s += t[f];
        s += t[i];
        reverse(all(s));
        if (check(s)) ++ans;
        // cout << "fuck " << i << ' ' << j << ' ' << s << endl;
      }
      else if (l == j) {
        for (int f = i; f != j; f = par[f][0])
          s += t[f];
        s += t[j];
        if (check(s)) ++ans;
        // cout << "fuck " << i << ' ' << j << ' ' << s << endl;
      }
      else {
        string aa = "";
        string bb = "";
        for (int f = i; f != l; f = par[f][0])
          aa += t[f];
        aa += t[l];
        for (int f = j; f != l; f = par[f][0])
          bb += t[f];
        reverse(all(bb));
        aa += bb;
        s = aa;
        if (check(aa)) ++ans;
        // cout << "fuck " << i << ' ' << j << ' ' << s << endl;
      }
    }
  cout << ans << '\n';
  return 0;
}

Compilation message

zagrade.cpp: In function 'bool check(std::string)':
zagrade.cpp:22:7: warning: unused variable 'n' [-Wunused-variable]
   22 |   int n = s.size();
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 174 ms 548 KB Output is correct
2 Correct 179 ms 548 KB Output is correct
3 Correct 173 ms 552 KB Output is correct
4 Correct 173 ms 348 KB Output is correct
5 Correct 170 ms 348 KB Output is correct
6 Correct 179 ms 344 KB Output is correct
7 Correct 192 ms 568 KB Output is correct
8 Correct 157 ms 548 KB Output is correct
9 Correct 174 ms 548 KB Output is correct
10 Correct 87 ms 552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 1480 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 174 ms 548 KB Output is correct
2 Correct 179 ms 548 KB Output is correct
3 Correct 173 ms 552 KB Output is correct
4 Correct 173 ms 348 KB Output is correct
5 Correct 170 ms 348 KB Output is correct
6 Correct 179 ms 344 KB Output is correct
7 Correct 192 ms 568 KB Output is correct
8 Correct 157 ms 548 KB Output is correct
9 Correct 174 ms 548 KB Output is correct
10 Correct 87 ms 552 KB Output is correct
11 Runtime error 2 ms 1480 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -