Submission #1077264

# Submission time Handle Problem Language Result Execution time Memory
1077264 2024-08-27T04:18:50 Z Ejen Lampice (COCI19_lampice) C++17
110 / 110
2963 ms 10800 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define el "\n"
#define fu(i, a, b) for (int i = a; i <= b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define sz(v) (ll)(v).size()
#define mask(i) (1LL << (i))
#define bit(x, i) ((x) >> (i) & 1)
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
ll Rand(ll l, ll r) {
    return uniform_int_distribution<ll>(l, r) (rng);
}
 
ll last(ll msk)     {return msk & (-msk);}
ll pop_cnt(ll msk)  {return __builtin_popcountll(msk);}
ll ctz(ll msk)      {return __builtin_ctzll(msk);}
ll lg(ll msk)       {return 63 - __builtin_clzll(msk);}
 
template<class T1, class T2> bool minimize(T1 &a, T2 b) {
    return a > b ? a = b, 1 : 0;
}
 
template<class T1, class T2> bool maximize(T1 &a, T2 b) {
    return a < b ? a = b, 1 : 0;
}
 
template<class T> void print(T a) {
    for (auto x : a) cout << x << " ";
    cout << el;
}
 
template<class T> void compress(T &a) {
    sort(all(a));
    a.resize(unique(all(a)) - a.begin());
}
 
const long long N = 5e4 + 2, mod = 1e9 + 7, inf = 2e18, bl = 320, lim = 1e6 + 27, base = 311;
 
int n, sz;
string s;
int child[N], h[N];
int hashUp[N], hashDown[N], pw[N], save[N];
bool del[N], ok[N];
basic_string<int> adj[N];
gp_hash_table <int, bool> mp;
 
void countChild(int u, int p) {
    child[u] = 1;
    // cout << u << ' '  << child[u] << el;
    for (int v : adj[u]) {
        if (v == p || del[v]) continue;
        countChild(v, u);
        child[u] += child[v];
    }
    // cout << child[u] << ' ';
}
 
int centroid(int u, int p, int root) {
    for (int v : adj[u]) {
        if (v == p || del[v] || child[v] <= child[root] / 2) continue;
        return centroid(v, u, root);
    }
    return u;
}
 
void dfs(int u, int p, int len) {
    if (h[u] > len) return;
    hashUp[u] = (1LL * s[u] * pw[h[u] - 1] + hashUp[p]) % mod;
    hashDown[u] = (1LL * hashDown[p] * base + s[u]) % mod;
    for (int v : adj[u]) {
        if (v == p || del[v]) continue;
        h[v] = h[u] + 1;
        dfs(v, u, len);
    }
}
 
void dfsUpdate(int u, int p, int len) {
    if (h[u] > len) return;
    if (2 * h[u] > len) {
        int par = 2 * h[u] - len;
        if (ok[par]) mp[(1LL * hashDown[u] - 1LL * save[par - 1] * pw[h[u] - par + 1] + mod * mod) % mod] = true;
    }
    if (h[u] > len) return;
    save[h[u]] = hashDown[u];
    if (hashDown[u] == hashUp[u]) ok[h[u]] = true;
    for (int v : adj[u]) {
        if (v == p || del[v]) continue;
        dfsUpdate(v, u, len);
    }
    ok[h[u]] = false;
}
 
bool dfsSearch(int u, int p, int len) {
    if (h[u] > len) return false;
    if (mp[hashDown[u]]) return true;
    if (h[u] == len && hashUp[u] == hashDown[u]) return true;
    for (int v : adj[u]) {
        if (v == p || del[v]) continue;
        if (dfsSearch(v, u, len)) return true;
    }
    return false;
}
 
bool check(int u, int len) {
    countChild(u, 0);
    int root = centroid(u, 0, u);
    h[root] = 1;
    dfs(root, 0, len);
    ok[1] = true;
    save[1] = hashDown[root];
    fu(turn, 0, 1) {
        mp.clear();
        for (int x : adj[root]) {
            if (del[x]) continue;
            if (dfsSearch(x, root, len)) return true;
            dfsUpdate(x, root, len);
        }
        reverse(all(adj[root]));
    }
    del[root] = true;
    for (int v : adj[root]) {
        if (del[v]) continue;
        if (check(v, len)) return true;;
    }
    return false;
}
 
signed main() {
//    freopen("bai3.inp", "r", stdin);
//    freopen("bai3.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    s = ' ' + s;
    sz = sz(s) - 1;
    pw[0] = 1;
    fu(i, 1, n) pw[i] = 1LL * pw[i - 1] * base % mod;
    fu(i, 2, n) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    // cout << check(1, 6);
    int res = 1;
    int l = 1, r = n / 2;
    while (l <= r) {
        int mid = (r + l) / 2;
        fu(i, 1, n) del[i] = 0;
        if (check(1, 2 * mid)) {
            res = 2 * mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    l = 1, r = n / 2;
    while (l <= r) {
        int mid = (l + r) / 2;
        fu(i, 1, n) del[i] = 0;
        if (check(1, 2 * mid + 1)) {
            maximize(res, 2 * mid + 1);
            l = mid + 1;
        }
        else r = mid - 1;
    }

    cout << res;
    cerr << "\nTime elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " ms.";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1880 KB Output is correct
2 Correct 7 ms 1884 KB Output is correct
3 Correct 29 ms 2140 KB Output is correct
4 Correct 28 ms 2136 KB Output is correct
5 Correct 1 ms 1880 KB Output is correct
6 Correct 1 ms 1880 KB Output is correct
7 Correct 1 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1330 ms 9904 KB Output is correct
2 Correct 1202 ms 10220 KB Output is correct
3 Correct 787 ms 10076 KB Output is correct
4 Correct 986 ms 10576 KB Output is correct
5 Correct 1612 ms 10800 KB Output is correct
6 Correct 137 ms 8920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2241 ms 7796 KB Output is correct
2 Correct 2963 ms 7568 KB Output is correct
3 Correct 2419 ms 7856 KB Output is correct
4 Correct 2171 ms 6392 KB Output is correct
5 Correct 1904 ms 7348 KB Output is correct
6 Correct 1811 ms 7332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1880 KB Output is correct
2 Correct 7 ms 1884 KB Output is correct
3 Correct 29 ms 2140 KB Output is correct
4 Correct 28 ms 2136 KB Output is correct
5 Correct 1 ms 1880 KB Output is correct
6 Correct 1 ms 1880 KB Output is correct
7 Correct 1 ms 1880 KB Output is correct
8 Correct 1330 ms 9904 KB Output is correct
9 Correct 1202 ms 10220 KB Output is correct
10 Correct 787 ms 10076 KB Output is correct
11 Correct 986 ms 10576 KB Output is correct
12 Correct 1612 ms 10800 KB Output is correct
13 Correct 137 ms 8920 KB Output is correct
14 Correct 2241 ms 7796 KB Output is correct
15 Correct 2963 ms 7568 KB Output is correct
16 Correct 2419 ms 7856 KB Output is correct
17 Correct 2171 ms 6392 KB Output is correct
18 Correct 1904 ms 7348 KB Output is correct
19 Correct 1811 ms 7332 KB Output is correct
20 Correct 1521 ms 5508 KB Output is correct
21 Correct 1630 ms 6936 KB Output is correct
22 Correct 2411 ms 6952 KB Output is correct
23 Correct 443 ms 3928 KB Output is correct
24 Correct 2059 ms 5460 KB Output is correct
25 Correct 1877 ms 5248 KB Output is correct
26 Correct 2410 ms 7992 KB Output is correct
27 Correct 2461 ms 7092 KB Output is correct
28 Correct 1466 ms 3932 KB Output is correct
29 Correct 1575 ms 4180 KB Output is correct
30 Correct 1739 ms 6660 KB Output is correct
31 Correct 1503 ms 5060 KB Output is correct
32 Correct 1281 ms 7088 KB Output is correct
33 Correct 1271 ms 7444 KB Output is correct