# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
257548 | 2020-08-04T11:59:11 Z | islingr | Amusement Park (JOI17_amusement_park) | C++17 | 0 ms | 0 KB |
#include "Ioi.h" #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); ++i) #define all(x) begin(x), end(x) using ll = long long; namespace { const int N = 1 << 14, B = 60; int nxt[N]; int head(int u) { return nxt[u] != u ? nxt[u] = head(nxt[u]) : u; } bool unite(int u, int v) { u = head(u); v = head(v); if (u == v) return 0; nxt[u] = v; return 1; } vector<int> g[N]; int p[N], in[N], timer = 0; void init(int u) { in[u] = timer; if (++timer == B) timer = 0; for (int v : g[u]) { g[v].erase(find(all(g[v]), u)); p[v] = u; init(v); } } const ll U = (1ll << B) - 1; int cnt; ll cur; void dummy_move() { ++cnt; } bool dummy_add(int u) { cur |= 1ll << in[u]; return cur == U; } bool dummy_dfs(int u) { dummy_add(u); if (cur == U) return 1; for (int v : g[u]) { dummy_move(); if (dummy_dfs(v)) return 1; dummy_move(); } return 0; } ll x = 0; bool add(int u, ll z) { cur |= 1ll << in[u]; x |= z << in[u]; return cur == U; } bool dfs(int u, ll z) { if (add(u, z)) return 1; for (int v : g[u]) { if (dfs(v, Move(v))) return 1; Move(u); } return 0; } } ll Ioi(int n, int m, int a[], int b[], int P, int V, int T) { rep(u, 0, n) nxt[u] = u; rep(e, 0, m) { if (unite(a[e], b[e])) { g[a[e]].push_back(b[e]); g[b[e]].push_back(a[e]); } } init(0); int s = -1; rep(i, 0, 2 * B + 1) { int u = P; cur = cnt = 0; rep(j, 0, i) dummy_add(u), dummy_move(), u = p[u]; if (!dummy_dfs(u)) continue; if (cnt > 2 * B) continue; s = u; break; } assert(0 <= s); cur = 0; while (P != s) add(P, V), V = Move(p[P]), P = p[P]; dfs(P, V); return x; }