Submission #1135032

#TimeUsernameProblemLanguageResultExecution timeMemory
1135032panPalindromi (COCI22_palindromi)C++20
110 / 110
383 ms195760 KiB
#include <bits/stdc++.h> //#include "includeall.h" //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define endl '\n' #define f first #define s second #define pb push_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define input(x) scanf("%lld", &x); #define input2(x, y) scanf("%lld%lld", &x, &y); #define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z); #define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a); #define print(x, y) printf("%lld%c", x, y); #define show(x) cerr << #x << " is " << x << endl; #define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl; #define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl; #define all(x) x.begin(), x.end() #define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); #define FOR(i, x, n) for (ll i =x; i<=n; ++i) #define RFOR(i, x, n) for (ll i =x; i>=n; --i) using namespace std; mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count()); //using namespace __gnu_pbds; //#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> //#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> typedef long long ll; typedef long double ld; typedef pair<ld, ll> pd; typedef pair<string, ll> psl; typedef pair<ll, ll> pi; typedef pair<ll, pi> pii; typedef pair<pi, pi> piii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct errtree { ll ans, nxtl, nxtr, _size; vector<ll> len, dp[2], to[2]; deque<ll> arr; errtree() { len.pb(-1); len.pb(0); // dummy nxtl = nxtr = 1; _size = 0, ans = 2; for (ll j=0; j<2; ++j) for (ll i=0; i<2; ++i) {dp[i].pb(0); to[i].pb(0);} } void insert(ll from, ll val, ll back) { len.pb(len[from] + 2); for (ll i=0; i<2; ++i) to[i].pb(0); to[val][from] = ans++; ll prevv = ((from==0)?1:to[val][dp[val][from]]); ll val2 = 0; if (back) val2 = arr[_size - len[prevv]]; else val2 = arr[len[prevv]]; dp[val2].pb(prevv); dp[val2^1].pb(dp[val2^1][prevv]); } void insert_right(ll val) { arr.pb(val); ll now; if (len[nxtr] < _size && arr[_size - len[nxtr] - 1] == val) now = nxtr; else now = dp[val][nxtr]; if (to[val][now]==0) insert(now, val, 1); ++_size; nxtr = to[val][now]; if (len[nxtr]==_size) nxtl = nxtr; } void insert_left(ll val) { ll now; if (len[nxtl] < _size &&arr[len[nxtl]] == val) now = nxtl; else now = dp[val][nxtl]; arr.push_front(val); if (to[val][now]==0) insert(now, val, 0); ++_size; nxtl= to[val][now]; if (len[nxtl]==_size) nxtr = nxtl; } }root[100005]; // UFDS ll par[100005]; ll find(ll x) {return ((x==par[x])?x:par[x] = find(par[x]));} int main() { ll n; string ss; cin >> n; cin >> ss; for (ll i=0; i<n; ++i) par[i] = i; for (ll i=0; i<n; ++i) root[i].insert_left(ss[i]-'0'); for (ll i=0; i<n-1; ++i) { ll u, v; cin >> u >> v; u--; v--; u = find(u); v = find(v); if (root[u]._size < root[v]._size) { for (ll j=0; j<root[u]._size; ++j) { root[v].insert_left(root[u].arr[root[u]._size-j-1]); } cout << root[v].ans-2 << endl; par[u] = v; } else { for (ll j=0; j<root[v]._size; ++j) { root[u].insert_right(root[v].arr[j]); } cout << root[u].ans-2 << endl; par[v] = u; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...