답안 #1021449

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1021449 2024-07-12T18:24:48 Z LOLOLO Lampice (COCI19_lampice) C++17
17 / 110
1694 ms 12896 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (ll)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 50000 + 10;
ll mod = 1e9 + 7;
ll pw[N];
ll base = 256;
map <ll, bool> mp[N];
ll a[N];
int n;
bool vis[N];
int sz[N];
vector <int> ed[N];

void dfs(int u, int v) {
    sz[u] = 1;
    for (auto x : ed[u]) {
        if (x == v || vis[x])
            continue;

        dfs(x, u);
        sz[u] += sz[x];
    }
}

int find_centroid(int u, int v, int ss) {
    for (auto x : ed[u]) {
        if (x == v || vis[x])
            continue;

        if (sz[x] * 2 > ss)
            return find_centroid(x, u, ss);
    }

    return u;
}

int mx = 0, len = 0, root = 0;

void run(int u, int v, int upd, int h, ll up, ll down, int &is) {
    mx = max(mx, h);
    up = (up * base + a[u]) % mod;
    down = (down + pw[h - 1] * a[u]) % mod;
    ll val = (down * pw[len - h] - up + mod) % mod;
    if (upd) {
        mp[h][val] = 1;
    } else {
        if (h + 1 == len && (down * base + root) % mod == up) {
            is = 1;
            return;
        }

        if (len > h && mp[len - h - 1].find(val) != mp[len - h - 1].end()) {
            is = 1;
            return;
        }
    }

    if (h < len) {
        for (auto x : ed[u]) {
            if (x == v || vis[x])
                continue;

            run(x, u, upd, h + 1, up, down, is);
            mx = max(mx, h + 1);
            if (is)
                return;
        }
    }
}

void process(int u, int &is) {
    dfs(u, 0);
    int cen = find_centroid(u, 0, sz[u]);
    vis[cen] = 1;
    root = a[u];
    mx = 0;

    for (auto x : ed[cen]) {
        if (vis[x])
            continue;

        run(x, 0, 0, 1, root, 0, is);
        if (is)
            return;

        run(x, 0, 1, 1, root, 0, is);
    }

    for (int i = 0; i <= mx; i++)
        mp[i].clear();

    for (auto x : ed[cen]) {
        if (vis[x])
            continue;

        process(x, is);
        if (is)
            return;
    }
}

bool check(int x) {
    for (int i = 0; i <= n; i++)
        mp[i].clear();

    mem(vis, 0);
    int is = 0;
    len = x;
    process(1, is);
    return is;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    string str;
    cin >> str;
    str = '0' + str;

    for (int i = 1; i <= n; i++)
        a[i] = (str[i] - 'a' + 1);

    pw[0] = 1;
    for (int i = 1; i <= n; i++)
        pw[i] = (pw[i - 1] * base) % mod; 

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }

    int ans = 1;
    for (int i = 0; i < 2; i++) {
        int l = 0, r = n / 2 + 1;
        while (r - l > 1) {
            int m = (l + r) / 2;
            if (check(m * 2 + i)) {
                ans = max(ans, m * 2 + i);
                l = m;
            } else {
                r = m;
            }
        }
    }


    cout << ans << '\n';
    return 0;
}

// 1, 2, .3, 4, 5, .6
// 
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3928 KB Output is correct
2 Correct 6 ms 3932 KB Output is correct
3 Correct 18 ms 4188 KB Output is correct
4 Correct 18 ms 4188 KB Output is correct
5 Correct 2 ms 3932 KB Output is correct
6 Correct 2 ms 3932 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 744 ms 12896 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1611 ms 11440 KB Output is correct
2 Incorrect 1694 ms 11020 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3928 KB Output is correct
2 Correct 6 ms 3932 KB Output is correct
3 Correct 18 ms 4188 KB Output is correct
4 Correct 18 ms 4188 KB Output is correct
5 Correct 2 ms 3932 KB Output is correct
6 Correct 2 ms 3932 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Incorrect 744 ms 12896 KB Output isn't correct
9 Halted 0 ms 0 KB -