Submission #1084633

# Submission time Handle Problem Language Result Execution time Memory
1084633 2024-09-06T15:10:04 Z anhthi Lampice (COCI19_lampice) C++14
0 / 110
5000 ms 9304 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)

template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }

template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }

template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a));
        a.resize(unique(all(a)) - a.begin());
    }

int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
    return l + abs((ll) rng()) % (r - l + 1);
}

const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7;

const int mxn = 5e4, mxm = 3e6;
const int LG = 18, BASE = 307;

int n;
char a[mxn + 5];
vector<int> g[mxn + 5];

int pw[mxn + 5];

namespace CTDL {

    bool del[mxn + 5];
    int childs[mxn + 5];

    int getChilds(int u, int p) {
        childs[u] = 1;
        for (int v : g[u]) if (v != p && !del[v])
            childs[u] += getChilds(v, u);
        return childs[u];
    }
    int getCen(int u, int p, int sz) {
        for (int v : g[u]) if (v != p && !del[v]) {
            if (2 * childs[v] > sz) return getCen(v, u, sz);
        }
        return u;
    }

    int par[mxn + 5];
    int f[mxn + 5], h[mxn + 5];
    void prepare(int u, int p, int d = 1) {
        for (int v : g[u]) if (v != p && !del[v]) {
            par[v] = u;
            f[v] = (f[u] * BASE + a[v] - 'a' + 1) % mod;
            h[v] = ((a[v] - 'a' + 1) * pw[d] + h[u]) % mod;

            prepare(v, u, d + 1);
        }
        return;
    }
    bool ans;
    map<int, bool> mark;

    int node[mxn + 5];
    void dfs(int u, int p, int k, bool flag, int d = 2) {
        if (d > k) return;
        node[d] = u;
        if (flag) {
            int rem = k - d;
            if (!rem) ans = ans || (f[u] == h[u]);
            if (d > rem) {
                int v = node[d - rem];
                if (f[v] == h[v])
                    ans = ans || mark[(f[u] - 1LL * f[par[v]] * pw[rem+1] + 1LL * mod * mod) % mod];
            }
        } else mark[f[u]] = true;
        for (int v : g[u]) if (v != p && !del[v]) {
            dfs(v, u, k, flag, d + 1);
        }
        return;
    }
    void reset(int u, int p, bool flag) {
        if (flag) f[u] = h[u] = par[u] = 0;
        for (int v : g[u]) if (v != p && !del[v]) {
            reset(v, u, flag);
        }
        return;
    }

    void calc(int rt, int k) {

//        cout << rt << ":\n";
        f[rt] = h[rt] = a[rt] - 'a' + 1;
        prepare(rt, 0);

        node[1] = rt;
        for (int v : g[rt]) if (!del[v]) {
            dfs(v, rt, k, true);
            dfs(v, rt, k, false);
        }
        reset(rt, 0, false);

        mark.clear();
        reverse(all(g[rt]));
        for (int v : g[rt]) if (!del[v]) {
            dfs(v, rt, k, true);
            dfs(v, rt, k, false);
        }
        mark.clear();

        reset(rt, 0, true);

        return;
    }
    void solve(int u, int k) {

        int sz = getChilds(u, 0);
        int cen = getCen(u, 0, sz);

        calc(cen, k);

        del[cen] = true;
        for (int v : g[cen]) if (!del[v])
            solve(v, k);

        return;
    }

    bool ok(int k) {

        ans = false;
        forn(i, 0, n) del[i] = 0;

        solve(1, k);
//        cout << "\n\n";

        return ans;
    }

}

int main(void) {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n;
    rep(i, n) cin >> a[i];
    rep(i, n - 1) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }

    pw[0] = 1;
    rep(i, n) pw[i] = 1LL * pw[i-1] * BASE % mod;

    int ans = 1;
    for (int l = 1, r = n / 2; l <= r; ) {
        int m = (l + r) >> 1;
        if (CTDL::ok(2 * m)) {
            maximize(ans, 2 * m); l = m + 1;
        }
        else r = m - 1;
    }
    for (int l = 1, r = n / 2; l <= r; ) {
        int m = (l + r) >> 1;
        if (CTDL::ok(2 * m + 1)) {
            maximize(ans, 2 * m + 1); l = m + 1;
        }
        else r = m - 1;
    }

    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2462 ms 9304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5061 ms 8028 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1624 KB Output isn't correct
2 Halted 0 ms 0 KB -