Submission #637444

#TimeUsernameProblemLanguageResultExecution timeMemory
637444vovamrLampice (COCI19_lampice)C++17
17 / 110
5067 ms9388 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]; // cout << v + 1 << " " << a[v] << " " << u[v] << " " << d[v] << '\n'; 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); } } bool found = 0; map<int,int> mp; ve<int> cur_path; inline void dfs(int v, int p, const int &l) { cur_path.pb(v); if (sz(cur_path) == l && u[v] == d[v]) { found = 1; // cout << "hui " << v + 1 << '\n'; } int x = l + 1 - sz(cur_path); // other length int id = sz(cur_path) - x; if (id >= 0 && id < sz(cur_path) && sz(cur_path) >= 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.count(H)) { // cout << "hui1 " << v + 1 << " " << sz(cur_path) << " " << x << '\n'; found = 1; } } } for (auto &to : gr[v]) { if (to == p || used[to]) continue; dfs(to, v, l); } cur_path.pop_back(); } inline void add(int v, int p, const int &delta) { mp[d[v]] += delta; if (!mp[d[v]]) mp.erase(d[v]); for (auto &to : gr[v]) if (to != p && !used[to]) { add(to, v, delta); } } inline void cd(int v, int p, const int &l) { used[v] = 1; // cout << "centroid " << v + 1 << ":" << '\n'; dfs1(v, v); hashes(v, v, 0); mp.clear(); for (auto &to : gr[v]) { if (to == p || used[to]) continue; add(to, v, +1); } cur_path = {v}; for (auto &to : gr[v]) { if (to == p || used[to]) continue; add(to, v, -1); dfs(to, v, l); add(to, v, +1); } for (auto &to : gr[v]) { if (to == p || used[to]) continue; int c = centr(to, v, sz[to]); cd(c, v, l); } } 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); } int answer = 0; int l, r, m, ans; // cd(0, 0, 3); // cout << '\n' << '\n'; // return; // even l = 1, r = n / 2, ans = 0; while (l <= r) { m = l + r >> 1; found = 0; fill(used, used + n, 0); cd(0, 0, 2 * m); if (found) ans = m, l = m + 1; else r = m - 1; } chmax(answer, 2 * ans); // odd l = 1, r = (n - 1) / 2, ans = 0; while (l <= r) { m = l + r >> 1; found = 0; fill(used, used + n, 0); cd(0, 0, 2 * m + 1); if (found) 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:175:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  175 |   m = l + r >> 1;
      |       ~~^~~
lampice.cpp:186:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  186 |   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...