Submission #547968

# Submission time Handle Problem Language Result Execution time Memory
547968 2022-04-12T05:12:50 Z Jomnoi Lampice (COCI19_lampice) C++17
0 / 110
5000 ms 11820 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int MAX_N = 5e4 + 10;
const int P = 31;
const int MOD = 1e9 + 9;

char S[MAX_N];
vector <int> graph[MAX_N];
long long expo[MAX_N];

class CentroidDecomposition {
private :
    int max_len;
    long long H[MAX_N], rH[MAX_N];
    int sz[MAX_N], depth[MAX_N], parent[MAX_N][16];
    bitset <MAX_N> processed;
    vector <int> centroid, tmp;
    unordered_set <int> mp;
public :
    int get_size(const int &u, const int &p) {
        sz[u] = 1;
        for(auto v : graph[u]) {
            if(v != p and processed[v] == false) {
                sz[u] += get_size(v, u);
            }
        }
        return sz[u];
    }

    int get_centroid(const int &u, const int &p, const int &n) {
        for(auto v : graph[u]) {
            if(v != p and processed[v] == false and sz[v] > n / 2) {
                return get_centroid(v, u, n);
            }
        }
        return u;
    }

    void build_centroid() {
        queue <int> q;
        q.push(1);
        while(!q.empty()) {
            int u = q.front();
            q.pop();

            int c = get_centroid(u, -1, get_size(u, -1));
            processed[c] = true;
            centroid.push_back(c);

            for(auto v : graph[c]) {
                if(processed[v] == false) {
                    q.push(v);
                }
            }
        }
    }

    int jump(int u, const int &k) {
        for(int i = 0; i < 16; i++) {
            if(k & (1<<i)) {
                u = parent[u][i];
            }
        }
        return u;
    }

    bool preprocess(const int &u, const int &p, const int &lvl) {
        depth[u] = lvl;
        if(depth[u] > max_len) {
            return false;
        }

        parent[u][0] = p;
        for(int i = 1; i < 16; i++) {
            parent[u][i] = parent[parent[u][i - 1]][i - 1];
        }

        H[u] = (H[p] * P + S[u]) % MOD;
        rH[u] = (rH[p] + S[u] * expo[lvl]) % MOD;
        if(max_len == lvl + 1 and H[u] == rH[u]) {
            return true;
        }
        for(auto v : graph[u]) {
            if(v != p and processed[v] == false and preprocess(v, u, lvl + 1) == true) {
                return true;
            }
        }
        return false;
    }

    bool dfs(int u, int p) {
        if(depth[u] > max_len) {
            return false;
        }

        // before substring + palindrome in current side + substring
        if(depth[u] >= max_len / 2) {
            int v = jump(u, max_len - depth[u] - 1);
            if(H[v] == rH[v] and mp.count(((H[u] - H[parent[v][0]] * expo[max_len - depth[u] - 1]) % MOD + MOD) % MOD)) {
                return true;
            }
        }

        tmp.push_back(H[u]);
        for(auto v : graph[u]) {
            if(v != p and processed[v] == false and dfs(v, u) == true) {
                return true;
            }
        }
        return false;
    }

    bool solve(const int &len) {
        if(len <= 1) {
            return true;
        }

        max_len = len;
        processed = 0;
        for(auto c : centroid) {
            processed[c] = true;
            H[c] = rH[c] = S[c];

            // Preprocess (Some operation that process once)
            for(auto v : graph[c]) {
                if(processed[v] == false and preprocess(v, c, 1) == true) {
                    return true;
                }
            }

            // Left to right
            mp.clear();
            for(auto v : graph[c]) {
                if(processed[v] == true) {
                    continue;
                }

                tmp.clear();
                if(dfs(v, c) == true) {
                    return true;
                }

                for(auto x : tmp) {
                    mp.insert(x);
                }
            }

            // Right to left
            mp.clear();
            for(int i = graph[c].size() - 1; i >= 0; i--) {
                int v = graph[c][i];
                if(processed[v] == true) {
                    continue;
                }

                tmp.clear();
                if(dfs(v, c) == true) {
                    return true;
                }

                for(auto x : tmp) {
                    mp.insert(x);
                }
            }
        }
        return false;
    }
}ct;

int main() {
    int N, l, r, mid, res, ans = 0;
    scanf(" %d %s", &N, S + 1);
    for(int i = 1; i <= N - 1; i++) {
        int A, B;
        scanf(" %d %d", &A, &B);
        graph[A].push_back(B);
        graph[B].push_back(A);
    }

    // Preprocess
    expo[0] = 1;
    for(int i = 1; i < MAX_N; i++) {
        expo[i] = (expo[i - 1] * P) % MOD;
    }
    for(int i = 1; i <= N; i++) {
        S[i] = S[i] - 'a' + 1;
    }

    ct.build_centroid();

    // Compute even length
    l = 0, r = N / 2, res = 0;
    while(l <= r) {
        mid = (l + r) / 2;

        if(ct.solve(2 * mid) == true) {
            res = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    ans = max(ans, 2 * res);

    // Compute odd length
    l = 0, r = N / 2, res = 0;
    while(l <= r) {
        mid = (l + r) / 2;

        if(ct.solve(2 * mid + 1) == true) {
            res = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    ans = max(ans, 2 * res + 1);

    printf("%d", ans);
    return 0;
}

Compilation message

lampice.cpp: In function 'int main()':
lampice.cpp:174:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |     scanf(" %d %s", &N, S + 1);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
lampice.cpp:177:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |         scanf(" %d %d", &A, &B);
      |         ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5073 ms 11820 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5039 ms 11224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1960 KB Output isn't correct
2 Halted 0 ms 0 KB -