Submission #1033975

#TimeUsernameProblemLanguageResultExecution timeMemory
1033975AmirAli_H1Split the Attractions (IOI19_split)C++17
100 / 100
78 ms37572 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 = 3e5 + 7; int n, m, R; vector<int> adj[maxn], adjx[maxn]; vector<pii> E, Ex; vector<int> res; int mark[maxn], p[maxn], sz[maxn]; vector<int> A, arr, val; bool cmp(int i, int j) { return (A[i] < A[j]); } void pre_dfs(int v, int par = -1) { mark[v] = 1; p[v] = par; sz[v] = 1; for (int u : adj[v]) { if (!mark[u]) { pre_dfs(u, v); sz[v] += sz[u]; Ex.pb(Mp(v, u)); } } } int get_cent(int v) { for (int u : adj[v]) { if (p[u] != v) continue; if (sz[u] * 2 > n) return get_cent(u); } return v; } int get(int a) { return (p[a] == a) ? a : p[a] = get(p[a]); } void merge(int u, int v) { int a = get(u), b = get(v); if (a == b) return ; if (sz[a] > sz[b]) { swap(u, v); swap(a, b); } p[a] = b; sz[b] += sz[a]; sz[a] = 0; adjx[u].pb(v); adjx[v].pb(u); } int get_vx(int v) { iota(p, p + n, 0); fill(sz, sz + n, 1); for (auto f : Ex) { if (f.F == v || f.S == v) continue; merge(f.F, f.S); } for (int i = 0; i < n; i++) { if (i == v || get(i) != i) continue; if (sz[i] >= val[0] && sz[i] <= val[0] + val[2]) return i; } for (auto f : E) { if (f.F == v || f.S == v) continue; merge(f.F, f.S); int i = get(f.F); if (sz[i] >= val[0] && sz[i] <= val[0] + val[2]) return i; } return -1; } void dfs_res(int v, int c) { mark[v] = 1; res[v] = c; R--; if (R == 0) return ; for (int u : adj[v]) { if (!mark[u]) { dfs_res(u, c); if (R == 0) return ; } } } vector<int> find_split(int Nx, int a, int b, int c, vector<int> ux, vector<int> vx) { n = Nx; m = len(ux); A = {a, b, c}; arr = {0, 1, 2}; sort(all(arr), cmp); val = {A[arr[0]], A[arr[1]], A[arr[2]]}; res.clear(); res.resize(n); for (int i = 0; i < m; i++) { int u = ux[i], v = vx[i]; adj[u].pb(v); adj[v].pb(u); E.pb(Mp(u, v)); } Ex.clear(); pre_dfs(0); int v = get_cent(0); int u = get_vx(v); if (u == -1) return res; fill(mark, mark + n, 0); fill(all(res), arr[2] + 1); for (int i = 0; i < n; i++) swap(adj[i], adjx[i]); R = val[0]; dfs_res(u, arr[0] + 1); for (int i = 0; i < n; i++) swap(adj[i], adjx[i]); R = val[1]; dfs_res(v, arr[1] + 1); 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...