/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 484.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 6ms 576.0 KiB
#6 Accepted 6ms 580.0 KiB
#7 Time Exceeded ≥3072ms ≥904.0 KiB
#8 Time Exceeded ≥3080ms ≥980.0 KiB

Code

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

void solve(int cs) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v, --u, --v;
    g[u].push_back(v);
    g[v].push_back(u); 
  }
  auto ispal = [&](string t) -> bool {
    for (int i = 0, j = t.size() - 1; j > i; i++, j--) if (t[i] != t[j]) return false;return true;
  };
  int q;
  cin >> q;
  while (q--) {
    int i;char c;
    cin >> i >> c;
    s[i - 1] = c;
    int res = 1;
    string t; t += s[0];
    auto dfs = [&](auto &&self, int node, int par) -> void {
      if (ispal(t)) {
        res = max(res, (int)t.size());
      }
      for (auto &nodes : g[node]) {
        if (nodes != par) {
          t += s[nodes];
          self(self, nodes, node);
          t.pop_back();
        }
      }
    };
    dfs(dfs, 0, -1);
    cout << res << "\n";
  }
  
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  // cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1045 Largest palindrome in tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-11 13:20:03
Judged At
2024-11-11 03:27:26
Judged By
Score
30
Total Time
≥3080ms
Peak Memory
≥980.0 KiB