답안 #198391

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198391 2020-01-25T19:02:18 Z alrad Lampice (COCI19_lampice) C++17
73 / 110
5000 ms 5856 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 1528 KB Output is correct
2 Correct 58 ms 1528 KB Output is correct
3 Correct 497 ms 1528 KB Output is correct
4 Correct 1298 ms 1656 KB Output is correct
5 Correct 3 ms 1528 KB Output is correct
6 Correct 3 ms 1528 KB Output is correct
7 Correct 3 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3292 KB Output is correct
2 Correct 19 ms 3320 KB Output is correct
3 Correct 19 ms 3448 KB Output is correct
4 Correct 20 ms 3320 KB Output is correct
5 Correct 21 ms 3576 KB Output is correct
6 Correct 20 ms 3568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1669 ms 4828 KB Output is correct
2 Correct 1403 ms 5112 KB Output is correct
3 Correct 2411 ms 5736 KB Output is correct
4 Correct 1504 ms 5856 KB Output is correct
5 Correct 1125 ms 4804 KB Output is correct
6 Correct 1244 ms 4600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 1528 KB Output is correct
2 Correct 58 ms 1528 KB Output is correct
3 Correct 497 ms 1528 KB Output is correct
4 Correct 1298 ms 1656 KB Output is correct
5 Correct 3 ms 1528 KB Output is correct
6 Correct 3 ms 1528 KB Output is correct
7 Correct 3 ms 1528 KB Output is correct
8 Correct 20 ms 3292 KB Output is correct
9 Correct 19 ms 3320 KB Output is correct
10 Correct 19 ms 3448 KB Output is correct
11 Correct 20 ms 3320 KB Output is correct
12 Correct 21 ms 3576 KB Output is correct
13 Correct 20 ms 3568 KB Output is correct
14 Correct 1669 ms 4828 KB Output is correct
15 Correct 1403 ms 5112 KB Output is correct
16 Correct 2411 ms 5736 KB Output is correct
17 Correct 1504 ms 5856 KB Output is correct
18 Correct 1125 ms 4804 KB Output is correct
19 Correct 1244 ms 4600 KB Output is correct
20 Execution timed out 5074 ms 3832 KB Time limit exceeded
21 Halted 0 ms 0 KB -