답안 #878761

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878761 2023-11-25T07:40:58 Z underwaterkillerwhale Lampice (COCI19_lampice) C++14
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,m,n) for(int i=m; i<=n; i++)
#define reb(i,m,n) for(int i=m; i>=n; i--)
#define iter(id, v) for (auto id : v)
#define ii pair<int,int>
#define fs first
#define se second
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
#define el cout <<"\n"

template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B> &v) { out << "(" << v.fs <<"," << v.se << ") "; return out; }

#ifndef ONLINE_JUDGE
    #include "debug.h"
#else
    #define deb(...) 23
    #define ____
#endif

mt19937 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }

const ll N = 5e4 + 7;
const ll Mod = 1e9 + 7;
const ll bse = 137;
const ll szBL = 650;
const ll INF = 2e9;

int n, m;

int a[N];

int nChild[N];
bool dd[N];
vector<int> ke[N];

ll hshdown[N], hshup[N], par[N], pw[N], dist[N];
set<ii> S;

bool res = 0;

void calc_dfs (ll u, ll pa) {
    nChild[u] = 1;
    iter (&v, ke[u]) {
        if (v != pa && !dd[v]) {
            calc_dfs(v, u);
            nChild[u] += nChild[v];
        }
    }
}

ll get_cen (ll u, ll pa, ll sz_Tree) {
    iter (&v, ke[u]) {
        if (v != pa && !dd[v] && nChild[v] > sz_Tree / 2) {
            return get_cen(v, u, sz_Tree);
        }
    }
    return u;
}

void dfs1 (ll u, ll pa, ll centroid, ll Len) {
    if (dist[u] > Len || res)
        return;
    dist[u] = dist[pa] + 1;
    if (pa == centroid)
        hshup[u] = a[u];
    else {
        hshup[u] = (a[u] * pw[dist[u] - 1] % Mod + hshup[pa]) % Mod;
    }
    hshdown[u] = (hshdown[pa] * bse % Mod + a[u]) % Mod;
    ll curHsh = (hshup[u] * pw[Len - dist[u] + 1] % Mod - hshdown[u] + Mod) % Mod;
    if (S.find({curHsh, Len - dist[u] + 1}) != S.end()) {
        res = 1;
    }
    iter (&v, ke[u]) {
        if (v == pa || dd[v])
            continue;
        dfs1(v, u, centroid, Len);
    }
}

void dfs2 (ll u, ll pa, ll centroid, ll Len) {
    if (dist[u] > Len || res)
        return;
    ll curHsh = (hshup[u] * pw[Len - dist[u] + 1] % Mod - hshdown[u] + Mod) % Mod;
    S.insert({curHsh, dist[u]});
    iter (&v, ke[u]) {
        if (v == pa || dd[v])
            continue;
        dfs2(v, u, centroid, Len);
    }
}

void decompose (ll u, ll Len) {
    if (res) return;
    calc_dfs(u, 0);
    ll centroid = get_cen(u, 0, nChild[u]);
    dd[centroid] = 1;

    hshup[centroid] = 0;
    hshdown[centroid] = a[centroid];
    dist[centroid] = 1;
    S.insert({(hshup[centroid] * pw[Len] % Mod - hshdown[centroid] + Mod) % Mod, 1});
    iter (&v, ke[centroid]) {
        if (!dd[v]) {
            dfs1(v, centroid, centroid, Len);
            dfs2(v, centroid, centroid, Len);
        }
    }
    set<ii> ().swap(S);
    iter (&v, ke[centroid]) {
        if (!dd[v]) {
            decompose(v, Len);
        }
    }
}

bool check (ll Len) {
    res = 0;
    rep (i, 1, n) {
        dd[i] = 0;
    }
    decompose(1, Len);
    return res;
}

ll BS (ll l, ll r) {
    while (l < r) {
        ll mid = l + r + 1 >> 1;
        if (check(mid * 2)) l = mid;
        else r = mid - 1;
    }
    return l * 2;
}

ll BS1 (ll l, ll r) {
    while (l < r) {
        ll mid = l + r + 1 >> 1;
        if (check(mid * 2 + 1)) l = mid;
        else r = mid - 1;
    }
    return l * 2 + 1;
}


void chloe_solution() {
    cin >> n;
    string s;
    cin >> s;
    rep (i, 1, n) {
        a[i] = s[i - 1] - '`';
    }
    rep (i, 2, n) {
        ll u, v;
        cin >> u >> v;
        ke[u].pb(v);
        ke[v].pb(u);
    }
    pw[0] = 1;
    rep (i, 1, n) {
        pw[i] = (pw[i - 1] * bse) % Mod;
    }
    cout << max(BS(0, n / 2), BS1(0, (n - 1) / 2));
}

int main() {
//     file("c");
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll num_Test = 1;
//    cin >> num_Test;
    while(num_Test--)
        chloe_solution();
}
/*
8
acddbbcd
1 6
6 7
6 3
3 4
4 5
5 2
8 5
*/

Compilation message

lampice.cpp:22:14: fatal error: debug.h: No such file or directory
   22 |     #include "debug.h"
      |              ^~~~~~~~~
compilation terminated.