제출 #1224930

#제출 시각아이디문제언어결과실행 시간메모리
1224930AmirAli_H1Split the Attractions (IOI19_split)C++17
100 / 100
96 ms30908 KiB
// In the name of Allah #include <bits/stdc++.h> #include "split.h" using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef complex<ld> cld; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define kill(x) cout << x << '\n', exit(0) #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = (1 << 18) + 4; int n, m; int Ax, Bx, Cx; vector<int> Rx = {1, 2, 3}; vector<int> adjx[maxn], adj[maxn]; vector<pii> E; vector<int> res; int mark[maxn], p[maxn], sz[maxn], R; int get(int a) { return (p[a] == a) ? a : p[a] = get(p[a]); } void merge(int a, int b) { a = get(a); b = get(b); if (a == b) return ; if (sz[a] > sz[b]) swap(a, b); p[a] = b; sz[b] += sz[a]; } void fix_abc() { if (Cx < Bx) { swap(Bx, Cx); swap(Rx[1], Rx[2]); } if (Cx < Ax) { swap(Ax, Cx); swap(Rx[0], Rx[2]); } if (Bx < Ax) { swap(Ax, Bx); swap(Rx[0], Rx[1]); } } void dfsx(int v) { mark[v] = 1; for (int u : adjx[v]) { if (!mark[u]) { adj[u].pb(v); adj[v].pb(u); dfsx(u); } } } void pre_dfs(int v, int p = -1) { sz[v] = 1; for (int u : adj[v]) { if (u == p) continue; pre_dfs(u, v); sz[v] += sz[u]; } } int get_cent(int v, int p, int SZ) { for (int u : adj[v]) { if (u == p) continue; if (sz[u] * 2 > SZ) return get_cent(u, v, SZ); } return v; } void res_dfs(int v, int fx) { if (R <= 0) return ; mark[v] = 1; res[v] = fx; R--; for (int u : adj[v]) { if (!mark[u]) res_dfs(u, fx); } } void col_dfs(int v, int p) { for (int u : adj[v]) { if (u == p) continue; merge(v, u); col_dfs(u, v); } } void calx(int v, int u) { fill(mark, mark + n, 0); fill(all(res), Rx[2]); mark[v] = 1; R = Ax; res_dfs(u, Rx[0]); mark[v] = 0; R = Bx; res_dfs(v, Rx[1]); return ; } void get_res(int v) { for (int u : adj[v]) { if (sz[get(u)] >= Ax) { calx(v, u); return ; } } } vector<int> find_split(int nx, int ax, int bx, int cx, vector<int> px, vector<int> qx) { n = nx; m = len(px); res.resize(n); Ax = ax; Bx = bx; Cx = cx; fix_abc(); for (int i = 0; i < m; i++) { int u = px[i], v = qx[i]; adjx[u].pb(v); adjx[v].pb(u); E.pb(Mp(u, v)); } dfsx(0); pre_dfs(0); int v = get_cent(0, -1, sz[0]); iota(p, p + n, 0); fill(sz, sz + n, 1); for (int u : adj[v]) col_dfs(u, v); for (int u : adj[v]) { if (sz[get(u)] >= Ax) { calx(v, u); return res; } } for (auto f : E) { int ux = f.F, vx = f.S; if (ux == v || vx == v || get(ux) == get(vx)) continue; merge(ux, vx); adj[ux].pb(vx); adj[vx].pb(ux); if (sz[get(ux)] >= Ax) { get_res(v); return res; } } return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...