Submission #702660

#TimeUsernameProblemLanguageResultExecution timeMemory
702660onjo0127Split the Attractions (IOI19_split)C++17
100 / 100
144 ms21208 KiB
#include "split.h" #include <bits/stdc++.h> using namespace std; const int MXN = 100009; bool vs[MXN]; vector<int> G[MXN], T[MXN]; int N, M, F1 = 1, F2 = 2, F3 = 3, A, B, C, sz[MXN], P[MXN], ans[MXN], cent, U[MXN], S[MXN], I[MXN]; int root(int x) { if(U[x] == x) return x; return U[x] = root(U[x]); } void merg(int u, int v) { u = root(u); v = root(v); if(u != v) U[u] = v, S[v] += S[u]; } void dfs1(int x) { vs[x] = 1; sz[x] = 1; for(auto& it: G[x]) if(!vs[it]) { dfs1(it); sz[x] += sz[it]; T[x].push_back(it); T[it].push_back(x); } } int fnd(int rt, int x) { int mxi = -1; for(auto &it: T[x]) { if(sz[it] > sz[x]) continue; if(mxi == -1 || sz[mxi] < sz[it]) mxi = it; } if(mxi == -1 || sz[mxi] * 2 <= sz[rt]) return x; return fnd(rt, mxi); } void dfs2(int x, int p, int c) { sz[x] = 1; P[x] = p; I[x] = c; for(auto& it: T[x]) if(it != p) { dfs2(it, x, x == p ? ++c : c); sz[x] += sz[it]; } } int lft; void grd(int x, int p, int col) { if(!lft || ans[x]) return; ans[x] = col; --lft; for(auto& it: T[x]) if(it != p) grd(it, x, col); } void mka(vector<int> &res, int x, int c, int r) { if(!lft) return; res[x] = r; --lft; for(auto& it: G[x]) if(ans[it] == c && res[it] != r) mka(res, it, c, r); } vector<int> makeans(bool fail) { if(fail) { vector<int> res(N, 0); return res; } vector<int> res(N, F3); for(int i=0; i<N; i++) { if(ans[i] == 1) { lft = A; mka(res, i, 1, F1); break; } } for(int i=0; i<N; i++) { if(ans[i] == 2) { lft = B; mka(res, i, 2, F2); break; } } return res; } void bfs(int st) { int cnt = 0; queue<int> que; que.push(st); while(cnt < A) { queue<int> Q; Q.push(que.front()); que.pop(); while(Q.size()) { int x = Q.front(); Q.pop(); if(ans[x] == 1) continue; ans[x] = 1; ++cnt; for(auto& it: T[x]) { if(it == cent) continue; if(!ans[it]) { ans[it] = -1; Q.push(it); } } for(auto& it: G[x]) if(I[x] != I[it]) { if(it == cent) continue; if(!ans[it]) { que.push(it); } } } } for(int i=0; i<N; i++) if(ans[i] == -1) ans[i] = 0; } vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) { if(a > b) swap(a, b), swap(F1, F2); if(a > c) swap(a, c), swap(F1, F3); if(b > c) swap(b, c), swap(F2, F3); A = a; B = b; C = c; N = n; M = p.size(); for(int i=0; i<M; i++) { G[p[i]].push_back(q[i]); G[q[i]].push_back(p[i]); } dfs1(0); cent = fnd(0, 0); dfs2(cent, cent, 0); for(int i=0; i<N; i++) { if(i == cent) continue; if(sz[i] >= A && N - sz[i] >= B) { lft = -1; grd(i, P[i], 1); for(int j=0; j<N; j++) if(!ans[j]) ans[j] = 2; return makeans(0); } } for(int i=0; i<N; i++) S[i] = 1, U[i] = i; for(int i=0; i<M; i++) { if(p[i] == cent || q[i] == cent) continue; merg(p[i], q[i]); } int st = -1; for(auto& it: T[cent]) if(S[root(it)] >= A) st = it; if(st == -1) return makeans(1); bfs(st); int c1 = 0, c2 = 0; for(int i=0; i<N; i++) { if(!ans[i]) { ans[i] = 2; ++c2; } else ++c1; } return makeans(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...
#Verdict Execution timeMemoryGrader output
Fetching results...