# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
952871 | abczz | Split the Attractions (IOI19_split) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "split.h"
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;
vector <ll> V;
vector <ll> adj[100000], E[200000];
bool ap[200000], ok;
vector <ll> G[200000], X[100000];
bool visited[100000], marked[100000];
array<ll, 2> T[3];
queue <ll> Q, P;
ll m, k, A[3], B[3], in[100000], low[100000], st[100000], sz[200000], F[200000], C[200000], cnt;
void tar(ll u, ll p) {
V.push_back(u);
low[u] = st[u] = ++cnt;
ll rch = 0;
for (auto v : adj[u]) {
if (!st[v]) {
++rch;
tar(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= st[u]) {
if (p != -1) ap[u] = 1;
while (!V.empty()) {
auto x = V.back();
V.pop_back();
X[x].push_back(k);
G[k].push_back(x);
if (x == v) break;
}
X[u].push_back(k);
G[k].push_back(u);
++k;
}
}
else if (v != p) {
low[u] = min(low[u], st[v]);
}
}
if (p == -1 & rch > 1) ap[u] = 1;
}
void dfs(ll u, ll w) {
visited[u] = 1;
for (auto v : adj[u]) {
if (!visited[v] && !C[v]) {
if (B[w] < A[w]) {
F[v] = w;
++B[w];
}
else F[v] = 2;
dfs(v, w);
}
}
}
void find(ll u, ll w) {
if (B[w] < A[w]) {
++B[w];
F[u] = w;
}
else F[u] = 2;
dfs(u, w);
for (auto v : adj[u]) {
if (C[v] && !visited[v]) {
find(v, w);
}
}
}
void solve(ll u, ll p) {
if (ap[u]) ++sz[u];
else sz[u] += G[u].size() - E[u].size();
ll mx = -1;
for (auto v : E[u]) {
if (v != p) {
solve(v, u);
sz[u] += sz[v];
mx = max(mx, sz[v]);
}
}
mx = max(mx, m - sz[u]);
if (!ok && !ap[u] && mx <= m-A[0]) {
if (mx >= A[1]) {
if (m-sz[u] == mx) {
F[p] = 1;
}
else {
for (auto v : E[u]) {
if (v != p) {
if (sz[v] == mx) {
F[v] = 1;
break;
}
}
}
}
for (auto v : G[u]) {
C[v] = 1;
}
for (auto v : G[u]) {
if (F[v]) {
++B[1];
dfs(v, 1);
}
}
for (auto v : G[u]) {
if (!F[v]) {
find(v, 0);
break;
}
}
}
else {
for (auto v : G[u]) {
C[v] = 1;
}
for (auto v : G[u]) {
for (auto x : adj[v]) {
if (C[x]) ++in[x];
}
}
visited[G[u][0]] = 1;
Q.push(G[u][0]);
while (!P.empty() || !Q.empty()) {
ll v;
if (!P.empty()) {
v = P.front();
P.pop();
}
else {
v = Q.front();
Q.pop();
if (marked[v]) continue;
}
marked[v] = 1;
if (B[0] < A[0]) {
++B[0];
F[v] = 0;
}
else F[v] = 2;
dfs(v, 0);
for (auto x : adj[v]) {
if (C[x]) {
--in[x];
if (!marked[x] && in[x] == 1) {
P.push(x);
visited[x] = 1;
}
else if (!visited[x] && in[x] > 1) {
Q.push(x);
visited[x] = 1;
}
}
}
}
for (int i=0; i<m; ++i) {
if (F[i] != 0) visited[i] = 0;
}
for (auto v : G[u]) {
if (F[v] == 2) {
find(v, 1);
break;
}
}
for (auto v : G[u]) {
if (!visited[v]) {
while (true) {
}
}
}
}
ok = 1;
return;
}
}