Submission #637445

#TimeUsernameProblemLanguageResultExecution timeMemory
637445vovamrLampice (COCI19_lampice)C++17
110 / 110
4750 ms10276 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fi first #define se second #define ll long long #define ld long double #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define pb push_back #define mpp make_pair #define ve vector using namespace std; using namespace __gnu_pbds; template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const ll inf = 1e18; const int iinf = 1e9; typedef pair<ll, ll> pll; typedef pair<int, int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); } template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); } constexpr int md = 1e9 + 9; constexpr int iv2 = (md + 1) / 2; inline int add(int a, int b) { a += b; if(a>=md){a-=md;} return a; } inline int sub(int a, int b) { a -= b; if(a<0){a+=md;} return a; } inline int mul(int a, int b) { return (ll)a * b % md; } inline int bp(int a, int n) { int res = 1; for (; n; n >>= 1, a = mul(a, a)) { if (n & 1) res = mul(res, a); } return res; } inline int inv(int a) { return bp(a, md - 2); } constexpr int p = 10039; const int N = 5e4 + 100; int pw[N]; inline void calc() { pw[0] = 1; for (int i = 1; i < N; ++i) pw[i] = mul(pw[i - 1], p); } int a[N]; ve<int> gr[N]; int used[N], sz[N]; inline void dfs1(int v, int p) { sz[v] = 1; for (auto &to : gr[v]) if (to != p && !used[to]) { dfs1(to, v); sz[v] += sz[to]; } } inline int centr(int v, int p, int n) { for (auto &to : gr[v]) if (to != p && !used[to] && sz[to] > n / 2) { return centr(to, v, n); } return v; } int u[N], d[N]; inline void hashes(int v, int p, int h) { if (v == p) u[v] = d[v] = a[v]; for (auto &to : gr[v]) if (to != p && !used[to]) { u[to] = add(u[v], mul(pw[h + 1], a[to])); d[to] = add(a[to], mul(pw[1], d[v])); hashes(to, v, h + 1); } } gp_hash_table<int,int> mp; int cur_path[N]; int S = 0; inline bool dfs(int v, int p, const int &l) { cur_path[S++] = v; if (S == l && u[v] == d[v]) return 1; int x = l + 1 - S; // other length int id = S - x; if (id >= 0 && id < S && S >= x) { int node = cur_path[id]; if (u[node] == d[node]) { int P = !id ? 0 : d[cur_path[id - 1]]; int H = sub(d[v], mul( P, pw[x] )); if (mp.find(H) != mp.end()) return 1; } } for (auto &to : gr[v]) { if (to == p || used[to]) continue; if (dfs(to, v, l)) return 1; } --S; return 0; } inline void add(int v, int p, const int &delta) { if (!(mp[d[v]] += delta)) mp.erase(d[v]); for (auto &to : gr[v]) if (to != p && !used[to]) { add(to, v, delta); } } inline bool cd(int v, int p, const int &l) { used[v] = 1; dfs1(v, v); hashes(v, v, 0); mp.clear(); for (auto &to : gr[v]) { if (to == p || used[to]) continue; add(to, v, +1); } S = 1; cur_path[0] = v; for (auto &to : gr[v]) { if (to == p || used[to]) continue; add(to, v, -1); S = 1; if (dfs(to, v, l)) return 1; add(to, v, +1); } for (auto &to : gr[v]) { if (to == p || used[to]) continue; int c = centr(to, v, sz[to]); if (cd(c, v, l)) return 1; } return 0; } inline void solve() { calc(); int n; cin >> n; for (int i = 0; i < n; ++i) { char x; cin >> x; a[i] = x - 'a' + 1; } for (int i = 1; i < n; ++i) { int v, u; cin >> v >> u, --v, --u; gr[v].pb(u), gr[u].pb(v); } dfs1(0, 0); int C = centr(0, 0, n); int answer = 0; int l, r, m, ans; // even l = 1, r = n / 2, ans = 0; while (l <= r) { m = l + r >> 1; fill(used, used + n, 0); if (cd(C, C, 2 * m)) ans = m, l = m + 1; else r = m - 1; } chmax(answer, 2 * ans); // odd l = ans, r = (n - 1) / 2, ans = 0; while (l <= r) { m = l + r >> 1; fill(used, used + n, 0); if (cd(C, C, 2 * m + 1)) ans = m, l = m + 1; else r = m - 1; } chmax(answer, 2 * ans + 1); cout << answer; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q = 1; // cin >> q; while (q--) solve(); cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl; }

Compilation message (stderr)

lampice.cpp: In function 'void solve()':
lampice.cpp:153:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  153 |   m = l + r >> 1;
      |       ~~^~~
lampice.cpp:162:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  162 |   m = l + r >> 1;
      |       ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...