Submission #198391

#TimeUsernameProblemLanguageResultExecution timeMemory
198391alradLampice (COCI19_lampice)C++17
73 / 110
5074 ms5856 KiB
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; const int N = 5e4 + 2; vector<vector<int> > g(N , vector<int>()); int ans = 0; string all; string walk = ""; bool good(string cur) { string other = cur; reverse(other.begin() , other.end()); return other == cur; } void dfs(int v , int p = -1) { walk.push_back(all[v - 1]); if (good(walk)) { ans = max(ans , (int)walk.size()); } for (int to : g[v]) { if (to != p) { dfs(to , v); walk.pop_back(); } } } //manacher int getMaxPalindromic(string text) { int N = text.size(); if(N == 0) return 0; N = 2*N + 1; //Position count int L[N]; //LPS Length Array L[0] = 0; L[1] = 1; int C = 1; //centerPosition int R = 2; //centerRightPosition int i = 0; //currentRightPosition int iMirror; //currentLeftPosition int maxLPSLength = 0; int maxLPSCenterPosition = 0; int start = -1; int end = -1; int diff = -1; for (i = 2; i < N; i++) { iMirror = 2*C-i; L[i] = 0; diff = R - i; if(diff > 0) L[i] = min(L[iMirror], diff); while ( ((i + L[i]) < N && (i - L[i]) > 0) && ( ((i + L[i] + 1) % 2 == 0) || (text[(i + L[i] + 1)/2] == text[(i - L[i] - 1)/2] ))) { L[i]++; } if(L[i] > maxLPSLength) { maxLPSLength = L[i]; maxLPSCenterPosition = i; } if (i + L[i] > R) { C = i; R = i + L[i]; } } start = (maxLPSCenterPosition - maxLPSLength)/2; end = start + maxLPSLength - 1; return end - start + 1; } vector<bool> lf(N , false); void dfs1(int v , int p = -1) { walk.push_back(all[v - 1]); if (lf[v]) { int cur = getMaxPalindromic(walk); ans = max(ans , cur); } for (int to : g[v]) { if (to != p) { dfs1(to , v); walk.pop_back(); } } } int main() { ios_base :: sync_with_stdio(0); cin.tie(0) , cout.tie(0); int n; cin >> n; cin >> all; bool subtask2 = true; for (int i = 1; i < n; i++) { int u , v; cin >> u >> v; if (u + 1 != v) { subtask2 = false; } g[u].push_back(v); g[v].push_back(u); } if (n <= 3000) { //solve subtask 1 for (int i = 1; i <= n; i++) { walk = ""; dfs(i); } cout << ans << '\n'; return 0; } if (subtask2) { //solve subtask 2 cout << getMaxPalindromic(all) << '\n'; return 0; } vector<int> leafs; for (int i = 1; i <= n; i++) { if ((int)g[i].size() == 1) { leafs.push_back(i); lf[i] = true; } } for (int i = 0; i < (int)leafs.size(); i++) { walk = ""; dfs1(leafs[i]); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...