Submission #1065590

# Submission time Handle Problem Language Result Execution time Memory
1065590 2024-08-19T09:43:13 Z Flan312 Lampice (COCI19_lampice) C++17
110 / 110
2599 ms 12196 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define ld long double
#define eb emplace_back
#define task ""
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
#define fi first
#define se second
#define pii pair <int, int>
#define tii tuple <int, int, int>
#define all(s) s.begin(), s.end()
using namespace std;
using namespace __gnu_pbds;

const ll base = 311;
const int nmax = 5e4 + 2;

ll pw[nmax];
int n;
string a;
basic_string <int> adj[nmax];
gp_hash_table <ll, bool> mp;

struct Hash
{
    ll hl[nmax], hr[nmax], pw[nmax];
    int len;

    void push_back(char c)
    {
        hl[len + 1] = hl[len] * base + c;
        hr[len + 1] = hr[len] + c * pw[len];
        ++len;
    }

    void pop_back()
    {
        --len;
    }

    void clear()
    {
        len = 0;
    }

    bool isPalin(int l)
    {
        return hl[l] == hr[l];
    }

    ll get(int l)
    {
        return hl[len] - hl[len - l] * pw[l];
    }

    Hash()
    {
        len = 0;
        pw[0] = 1;
        for (int i = 1; i < nmax; ++i)
            pw[i] = pw[i - 1] * base;
        
    }
} hs;

struct centroidDecomposition
{
    int sz[nmax];
    bool del[nmax];

    void init()
    {
        memset(del, 0, (n + 1) * sizeof(bool));
    }

    void dfs(int u, int p)
    {
        sz[u] = 1;
        for (auto &v : adj[u])
        {
            if (v == p || del[v]) continue;
            dfs(v, u);
            sz[u] += sz[v];
        }
    }

    int findCentroid(int u, int p, int total)
    {
        for (auto &v : adj[u])
        {
            if (v == p || del[v]) continue;
            if (sz[v] > (total >> 1))
                return findCentroid(v, u, total);
        }
        return u;
    }

    bool dfs_update(int u, int p, int depth, int len)
    {
        hs.push_back(a[u]);
        if (depth * 2 >= len && hs.isPalin(2 * depth - len)) //duong di qua goc tu 2 dinh con cach goc 1 khoang depth
            mp[hs.get(len - depth)] = 1; 
        if (depth == len && hs.isPalin(len))
            return 1;
        for (auto &v : adj[u])
        {
            if (v == p || del[v]) continue;
            if (dfs_update(v, u, depth + 1, len)) return 1;
        }
        hs.pop_back();
        return 0;
    }

    bool dfs_search(int u, int p, int depth, int len)
    {
        hs.push_back(a[u]);
        if (depth * 2 <= len && mp[hs.get(depth)]) //chi 1 nhanh
            return 1;
        if (depth == len && hs.isPalin(len))
            return 1;
        for (auto &v : adj[u])
        {
            if (v == p || del[v]) continue;
            if (dfs_search(v, u, depth + 1, len)) return 1;
        }
        hs.pop_back();
        return 0;
    }

    bool build(int u, int len)
    {
        dfs(u, -1);
        int centroid = findCentroid(u, -1, sz[u]);
        del[centroid] = 1;

        for (int turn = 0; turn <= 1; ++turn)
        {
            mp.clear();
            for (auto &v : adj[centroid])
            {
                if (del[v]) continue;
                
                hs.clear();
                if (dfs_search(v, centroid, 1, len)) 
                    return 1;
            
                hs.clear();
                hs.push_back(a[centroid]);
                if (dfs_update(v, centroid, 2, len)) 
                    return 1;
            }

            reverse(all(adj[centroid]));
        }

        for (auto &v : adj[centroid])
        {
            if (del[v]) continue;
            if (build(v, len)) return 1;
        }

        return 0;
    }
} cd;

bool check(int mid)
{
    cd.init();
    return cd.build(1, mid);
}

int main()
{
    if (ifstream(task".inp")) nx
    fast

    cin >> n >> a;
    a = ' ' + a;

    for (int u, v, i = 1; i < n; ++i)
    {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

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

    l = 1, r = (n >> 1);
    while(l <= r)
    {
        int mid = l + r >> 1;
        if (check(2 * mid + 1))
        {
            ans = max(ans, 2 * mid + 1);
            l = mid + 1;
        }
        else r = mid - 1;
    }
    cout << ans;
}

Compilation message

lampice.cpp: In function 'int main()':
lampice.cpp:192:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  192 |         int mid = l + r >> 1;
      |                   ~~^~~
lampice.cpp:204:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  204 |         int mid = l + r >> 1;
      |                   ~~^~~
lampice.cpp:8:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:176:31: note: in expansion of macro 'nx'
  176 |     if (ifstream(task".inp")) nx
      |                               ^~
lampice.cpp:8:52: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
      |                                            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:176:31: note: in expansion of macro 'nx'
  176 |     if (ifstream(task".inp")) nx
      |                               ^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2392 KB Output is correct
2 Correct 8 ms 2648 KB Output is correct
3 Correct 27 ms 2652 KB Output is correct
4 Correct 25 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1470 ms 8760 KB Output is correct
2 Correct 1095 ms 8900 KB Output is correct
3 Correct 722 ms 11652 KB Output is correct
4 Correct 896 ms 11932 KB Output is correct
5 Correct 1501 ms 12196 KB Output is correct
6 Correct 104 ms 8928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2361 ms 10152 KB Output is correct
2 Correct 2599 ms 10132 KB Output is correct
3 Correct 2120 ms 10404 KB Output is correct
4 Correct 1719 ms 7508 KB Output is correct
5 Correct 1695 ms 9988 KB Output is correct
6 Correct 1708 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2392 KB Output is correct
2 Correct 8 ms 2648 KB Output is correct
3 Correct 27 ms 2652 KB Output is correct
4 Correct 25 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1470 ms 8760 KB Output is correct
9 Correct 1095 ms 8900 KB Output is correct
10 Correct 722 ms 11652 KB Output is correct
11 Correct 896 ms 11932 KB Output is correct
12 Correct 1501 ms 12196 KB Output is correct
13 Correct 104 ms 8928 KB Output is correct
14 Correct 2361 ms 10152 KB Output is correct
15 Correct 2599 ms 10132 KB Output is correct
16 Correct 2120 ms 10404 KB Output is correct
17 Correct 1719 ms 7508 KB Output is correct
18 Correct 1695 ms 9988 KB Output is correct
19 Correct 1708 ms 9728 KB Output is correct
20 Correct 1224 ms 6672 KB Output is correct
21 Correct 1487 ms 9876 KB Output is correct
22 Correct 2193 ms 9676 KB Output is correct
23 Correct 364 ms 3768 KB Output is correct
24 Correct 1652 ms 5768 KB Output is correct
25 Correct 1624 ms 5456 KB Output is correct
26 Correct 2270 ms 10648 KB Output is correct
27 Correct 2557 ms 9772 KB Output is correct
28 Correct 1253 ms 3692 KB Output is correct
29 Correct 1314 ms 3796 KB Output is correct
30 Correct 1389 ms 6000 KB Output is correct
31 Correct 1300 ms 5336 KB Output is correct
32 Correct 1033 ms 7712 KB Output is correct
33 Correct 1201 ms 10172 KB Output is correct