답안 #1098457

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098457 2024-10-09T12:17:48 Z f0rizen Lampice (COCI19_lampice) C++17
42 / 110
5000 ms 11516 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;

template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
    for (auto &i : a) {
        is >> i;
    }
    return is;
}

const int P = 227, mod = 1e9 + 9;

int mul(int a, int b) {
    return (a * 1ll * b) % mod;
}

int sub(int a, int b) {
    return ((a - b) % mod + mod) % mod;
}

vector<int> pw;

vector<int> a;
vector<vector<int>> g;
vector<bool> used;
vector<int> sz;
bool ok = false;
vector<vector<int>> dp, rdp;
vector<int> up;
__gnu_pbds::gp_hash_table<int, bool> mp;

void dfs_sz(int v, int p = -1) {
    sz[v] = 1;
    for (auto u : g[v]) {
        if (u != p && !used[u]) {
            dfs_sz(u, v);
            sz[v] += sz[u];
        }
    }
}

void dfs_dp(int v, int p = -1, int d = 0) {
    dp[0][v] = (mul(dp[0][p], P) + a[v]) % mod;
    rdp[0][v] = (mul(a[v], pw[d - 1]) + rdp[0][p]) % mod;
    dp[1][v] = (mul(dp[1][p], P) + a[v]) % mod;
    rdp[1][v] = (mul(a[v], pw[d]) + rdp[1][p]) % mod;
    for (auto u : g[v]) {
        if (u != p && !used[u]) {
            dfs_dp(u, v, d + 1);
        }
    }
}

void dfs_ans(int len, int v, int p = -1, int d = 0) {
    up.push_back(v);
    int k = len - d - 1;
    if (k > 0) {
        if (len - 2 * k > 0) {
            int u = up[(int) up.size() - (k + 1)];
            if (dp[1][u] == rdp[1][u]) {
                int h = sub(dp[1][v], mul(pw[k], dp[1][u]));
                if (mp.find(h) != mp.end()) {
                    ok = true;
                }
            }
        } else if (len - 2 * k == 0) {
            int h = dp[1][v];
            if (mp.find(h) != mp.end()) {
                ok = true;
            }
        }
    } else if (k == 0) {
        if (dp[1][v] == rdp[1][v]) {
            ok = true;
        }
    }
    for (auto u : g[v]) {
        if (u != p && !used[u]) {
            dfs_ans(len, u, v, d + 1);
        }
    }
    up.pop_back();
}

void dfs_insert(int v, int p = -1) {
    mp[dp[0][v]] = 1;
    for (auto u : g[v]) {
        if (u != p && !used[u]) {
            dfs_insert(u, v);
        }
    }
}

void dfs_clear(int v, int p = -1) {
    dp[0][v] = 0;
    rdp[0][v] = 0;
    dp[1][v] = 0;
    rdp[1][v] = 0;
    for (auto u : g[v]) {
        if (u != p && !used[u]) {
            dfs_clear(u, v);
        }
    }
}

int centroid(int v, int p, int n) {
    for (auto u : g[v]) {
        if (u != p && !used[u]) {
            if (sz[u] * 2 > n) {
                return centroid(u, v, n);
            }
        }
    }
    return v;
}

void build(int v, int len) {
    dfs_sz(v);
    if (len == 1) {
        ok = true;
    }
    dp[1][v] = a[v];
    rdp[1][v] = a[v];
    for (auto u : g[v]) {
        if (!used[u]) {
            dfs_dp(u, v, 1);
        }
    }
    up.push_back(v);
    for (auto u : g[v]) {
        if (!used[u]) {
            dfs_ans(len, u, v, 1);
            dfs_insert(u, v);
        }
    }
    up.pop_back();
    mp.clear();
    up.push_back(v);
    for (int i = (int) g[v].size() - 1; i >= 0; --i) {
        int u = g[v][i];
        if (!used[u]) {
            dfs_ans(len, u, v, 1);
            dfs_insert(u, v);
        }
    }
    up.pop_back();
    mp.clear();
    dfs_clear(v);
    used[v] = 1;
    for (auto u : g[v]) {
        if (!used[u]) {
            build(centroid(u, v, sz[u]), len);
        }
    }
}

int32_t main() {
#ifdef LOCAL
    freopen("/tmp/input.txt", "r", stdin);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int n;
    cin >> n;
    pw.resize(n);
    pw[0] = 1;
    for (int i = 1; i < n; ++i) {
        pw[i] = mul(pw[i - 1], P);
    }
    a.resize(n);
    for (int i = 0; i < n; ++i) {
        char c;
        cin >> c;
        a[i] = c - 'a' + 1;
    }
    g.resize(n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    used.resize(n);
    sz.resize(n);
    dp.resize(2, vector<int>(n));
    rdp.resize(2, vector<int>(n));
    dfs_sz(0);
    int c = centroid(0, -1, sz[0]);
    int ans = 0;
    {
        int l = 0, r = n / 2 + 1;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (2 * mid > n) {
                r = mid;
                continue;
            }
            ok = false;
            fill(used.begin(), used.end(), 0);
            build(c, 2 * mid);
            if (ok) {
                l = mid;
            } else {
                r = mid;
            }
        }
        ans = max(ans, 2 * l);
    }
    {
        int l = 0, r = n / 2 + 1;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (2 * mid + 1 > n) {
                r = mid;
                continue;
            }
            ok = false;
            fill(used.begin(), used.end(), 0);
            build(c, 2 * mid + 1);
            if (ok) {
                l = mid;
            } else {
                r = mid;
            }
        }
        ans = max(ans, 2 * l + 1);
    }
    cout << ans << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 9 ms 348 KB Output is correct
3 Correct 39 ms 604 KB Output is correct
4 Correct 60 ms 600 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2579 ms 10156 KB Output is correct
2 Correct 2462 ms 10812 KB Output is correct
3 Correct 2636 ms 10868 KB Output is correct
4 Correct 2656 ms 11220 KB Output is correct
5 Correct 2930 ms 11516 KB Output is correct
6 Correct 1856 ms 9948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5046 ms 8520 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 9 ms 348 KB Output is correct
3 Correct 39 ms 604 KB Output is correct
4 Correct 60 ms 600 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 2579 ms 10156 KB Output is correct
9 Correct 2462 ms 10812 KB Output is correct
10 Correct 2636 ms 10868 KB Output is correct
11 Correct 2656 ms 11220 KB Output is correct
12 Correct 2930 ms 11516 KB Output is correct
13 Correct 1856 ms 9948 KB Output is correct
14 Execution timed out 5046 ms 8520 KB Time limit exceeded
15 Halted 0 ms 0 KB -