제출 #143897

#제출 시각아이디문제언어결과실행 시간메모리
143897onjo0127Split the Attractions (IOI19_split)C++14
40 / 100
202 ms19324 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]; 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) { sz[x] = 1; P[x] = p; for(auto& it: T[x]) if(it != p) { dfs2(it, x); sz[x] += sz[it]; } } int lft; void grd(int x, int p, int bck, int col) { if(!lft || ans[x]) return; ans[x] = col; --lft; if(bck) for(auto& it: G[x]) grd(it, x, bck, col); else for(auto& it: T[x]) if(it != p) grd(it, x, bck, col); } 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) res[i] = F1; if(ans[i] == 2) res[i] = F2; } return res; } void bfs(int st) { int cnt = 0; deque<int> que; que.push_back(st); ans[st] = -1; while(cnt < A) { int x = que.front(); que.pop_front(); ans[x] = 1; ++cnt; for(auto& it: T[x]) { if(it == cent) continue; if(!ans[it]) { ans[it] = -1; que.push_front(it); } } for(auto& it: G[x]) { if(it == cent) continue; if(!ans[it]) { ans[it] = -1; que.push_back(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); for(int i=1; i<N; i++) { if(sz[i] >= A && N - sz[i] >= B) { lft = A; grd(i, P[i], 0, 1); lft = B; grd(P[i], P[i], 1, 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); lft = B; grd(cent, cent, 1, 2); 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...