/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 580.0 KiB
#2 Accepted 1ms 584.0 KiB
#3 Accepted 2ms 580.0 KiB
#4 Accepted 2ms 576.0 KiB
#5 Accepted 10ms 580.0 KiB
#6 Accepted 10ms 580.0 KiB
#7 Time Exceeded ≥3061ms ≥1.867 MiB
#8 Time Exceeded ≥3078ms ≥1.871 MiB

Code

#include <bits/stdc++.h> 

using namespace std;

const int mxN = 1003;
vector<vector<int>> g(mxN);
string s;
int res;

bool ispal(string t) {
  for (int i = 0, j = t.size() - 1; j > i; i++, j--) if (t[i] != t[j]) return false; return true;
}

void dfs(int node, int par, string path) {
  path += s[node];
  if (ispal(path)) res = max(res, (int)path.size()); 
  for (auto &next_node : g[node]) {
    if (next_node != par) {
      dfs(next_node, node, path);
    }
  }
  path.pop_back();
}

void solve() {
  int n, q;
  cin >> n >> s;
  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);
  }
  cin >> q;
  while (q--) {
    int x; char c; cin >> x >> c;
    s[x - 1] = c;
    string t;
    res = 0;
    dfs(0, -1, t);
    cout << res << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  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:41:17
Judged At
2024-11-11 03:27:24
Judged By
Score
30
Total Time
≥3078ms
Peak Memory
≥1.871 MiB