Submission #198381

# Submission time Handle Problem Language Result Execution time Memory
198381 2020-01-25T17:58:54 Z alrad Lampice (COCI19_lampice) C++17
42 / 110
1205 ms 10208 KB
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

const int N = 5e4 + 2;

int n;
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();
      }
   }
}

//hashing

int gen_base(const int before , const int after) {
   auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
   mt19937 mt_rand(seed);
   int base = uniform_int_distribution<int>(before + 1, after)(mt_rand);
   return base % 2 == 0 ? base - 1 : base;
}

struct PolyHash {
   static const int mod = (int)1e9 + 123;
   static vector<int> pow1;
   static vector<ull> pow2;
   static int base;

   static inline int diff(int a , int b) {
      return (a -= b) < 0 ? a + mod : a;
   }

   vector<int> pref1;
   vector<ull> pref2;

   PolyHash(const string s) : pref1(s.size() + 1u , 0) , pref2(s.size() + 1u , 0) {
      assert(base < mod);
      const int n = s.size();
      while ((int)pow1.size() <= n) {
         pow1.push_back(1LL * pow1.back() * base % mod);
         pow2.push_back(pow2.back() * base);
      }
      for (int i = 0; i < n; i++) {
         assert(base > s[i]);
         pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
         pref2[i + 1] = pref2[i] + s[i] * pow2[i];
      }
   }

   inline pair<int , ull> operator()(const int pos , const int len , const int mxPow = 0) const {
      int hash1 = pref1[pos + len] - pref1[pos];
      ull hash2 = pref2[pos + len] - pref2[pos];
      if (hash1 < 0) hash1 += mod;
      if (mxPow != 0) {
         hash1 = 1LL * hash1 * pow1[mxPow - (pos + len - 1)] % mod;
         hash2 *= pow2[mxPow - (pos + len - 1)];
      }
      return make_pair(hash1 , hash2);
   }
};

int PolyHash::base((int)1e9 + 7);
vector<int> PolyHash::pow1{1};
vector<ull> PolyHash::pow2{2};

int getMaxPalindromic(string a) {
   int result = 0;
   PolyHash::base = gen_base(256 , PolyHash::mod);
   string b(a);
   reverse(b.begin() , b.end());
   PolyHash hash_a(all) , hash_b(b);
   for (int i = 0 , j = n - 1; i < n; i++ , j--) {
      int low = 0 , high = min(n - i , n - j) + 1;
      while (high - low > 1) {
         int mid = (low + high) >> 1;
         if (hash_a(i , mid , n) == hash_b(j , mid , n)) {
            low = mid;
         } else {
            high = mid;
         }
      }
      result = max(result , 2 * low - 1);
      low = 0 , high = min(n - i - 1 , n - j) + 1;
      while (high - low > 1) {
         int mid = (high + low) >> 1;
         if (hash_a(i + 1 , mid , n) == hash_b(j , mid , n)) {
            low = mid;
         } else {
            high = mid;
         }
      }
      result = max(result , 2 * low);
   }
   return result;
}

vector<bool> lf(N , false);

void dfs1(int v , int p = -1) {
   walk.push_back(all[v - 1]);
   if (lf[v]) {
      ans = max(ans , getMaxPalindromic(walk));
   }
   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);
   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 time Memory Grader output
1 Correct 18 ms 1528 KB Output is correct
2 Correct 62 ms 1656 KB Output is correct
3 Correct 479 ms 1524 KB Output is correct
4 Correct 1205 ms 1656 KB Output is correct
5 Correct 3 ms 1528 KB Output is correct
6 Correct 4 ms 1528 KB Output is correct
7 Correct 3 ms 1528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4740 KB Output is correct
2 Correct 27 ms 4692 KB Output is correct
3 Correct 29 ms 4780 KB Output is correct
4 Correct 36 ms 4896 KB Output is correct
5 Correct 35 ms 5076 KB Output is correct
6 Correct 38 ms 5104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 81 ms 10208 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 1528 KB Output is correct
2 Correct 62 ms 1656 KB Output is correct
3 Correct 479 ms 1524 KB Output is correct
4 Correct 1205 ms 1656 KB Output is correct
5 Correct 3 ms 1528 KB Output is correct
6 Correct 4 ms 1528 KB Output is correct
7 Correct 3 ms 1528 KB Output is correct
8 Correct 33 ms 4740 KB Output is correct
9 Correct 27 ms 4692 KB Output is correct
10 Correct 29 ms 4780 KB Output is correct
11 Correct 36 ms 4896 KB Output is correct
12 Correct 35 ms 5076 KB Output is correct
13 Correct 38 ms 5104 KB Output is correct
14 Runtime error 81 ms 10208 KB Execution killed with signal 11 (could be triggered by violating memory limits)
15 Halted 0 ms 0 KB -