# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
198383 |
2020-01-25T18:16:58 Z |
alrad |
Lampice (COCI19_lampice) |
C++17 |
|
5000 ms |
5632 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();
}
}
}
//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;
const int nx = (int)a.size();
PolyHash::base = gen_base(256 , PolyHash::mod);
string b = a;
reverse(b.begin() , b.end());
PolyHash hash_a(a) , hash_b(b);
for (int i = 0 , j = nx - 1; i < nx; i++ , j--) {
int low = 0 , high = min(nx - i , nx - j) + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (hash_a(i , mid , nx) == hash_b(j , mid , nx)) {
low = mid;
} else {
high = mid;
}
}
result = max(result , 2 * low - 1);
low = 0 , high = min(nx - i - 1 , nx - j) + 1;
while (high - low > 1) {
int mid = (high + low) / 2;
if (hash_a(i + 1 , mid , nx) == hash_b(j , mid , nx)) {
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]) {
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 time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1528 KB |
Output is correct |
2 |
Correct |
56 ms |
1656 KB |
Output is correct |
3 |
Correct |
479 ms |
1784 KB |
Output is correct |
4 |
Correct |
1237 ms |
1656 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1532 KB |
Output is correct |
7 |
Correct |
3 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
4512 KB |
Output is correct |
2 |
Correct |
24 ms |
4720 KB |
Output is correct |
3 |
Correct |
26 ms |
4724 KB |
Output is correct |
4 |
Correct |
37 ms |
4892 KB |
Output is correct |
5 |
Correct |
38 ms |
5104 KB |
Output is correct |
6 |
Correct |
37 ms |
5104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5038 ms |
5632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1528 KB |
Output is correct |
2 |
Correct |
56 ms |
1656 KB |
Output is correct |
3 |
Correct |
479 ms |
1784 KB |
Output is correct |
4 |
Correct |
1237 ms |
1656 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1532 KB |
Output is correct |
7 |
Correct |
3 ms |
1528 KB |
Output is correct |
8 |
Correct |
33 ms |
4512 KB |
Output is correct |
9 |
Correct |
24 ms |
4720 KB |
Output is correct |
10 |
Correct |
26 ms |
4724 KB |
Output is correct |
11 |
Correct |
37 ms |
4892 KB |
Output is correct |
12 |
Correct |
38 ms |
5104 KB |
Output is correct |
13 |
Correct |
37 ms |
5104 KB |
Output is correct |
14 |
Execution timed out |
5038 ms |
5632 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |