제출 #535417

#제출 시각아이디문제언어결과실행 시간메모리
535417mario05092929Split the Attractions (IOI19_split)C++14
100 / 100
288 ms25112 KiB
#include "split.h" #include <bits/stdc++.h> #define x first #define y second using namespace std; typedef long long ll; typedef pair <int,int> pi; typedef vector <int> vec; vector <int> rv[100005]; vector <int> v[100005]; int n,m,A,B,C,a,b; int cnt,cntmx; int c[100005],sz[100005],p[100005],szp[100005]; int ch[100005],poham[100005]; pi po[3]; vector <int> ans; vector <int> tv[100005]; vector <int> com,dfscom; int Find(int x) {return (x == p[x] ? x : p[x] = Find(p[x]));} void init_union() { for(int i = 0;i < n;i++) { p[i] = i, szp[i] = 1; } } int get_sz(int x,int pp) { sz[x] = 1; for(int i : tv[x]) { if(i == pp) continue; sz[x] += get_sz(i,x); } return sz[x]; } int get_cent(int x,int pp,int cap) { for(int i : tv[x]) if(i != pp&&sz[i] > cap) return get_cent(i,x,cap); return x; } int make_cent() { return get_cent(0,-1,get_sz(0,-1)/2); } void dfs_fill(int x,int pp,int color) { if(cnt == cntmx||ans[x]) return; cnt++; ans[x] = color; for(int i : tv[x]) { if(i == pp) continue; dfs_fill(i,x,color); } } void dfs_pofill(int x,int color,int isok) { if(cnt == cntmx||ans[x]||poham[x] != isok) return; cnt++; ans[x] = color; for(int i : rv[x]) { dfs_pofill(i,color,isok); } } void canDo(int x,int wi) { cnt = 0, cntmx = a; dfs_fill(wi,x,po[0].y); cnt = 0, cntmx = b; dfs_fill(x,wi,po[1].y); for(int i = 0;i < n;i++) { if(!ans[i]) ans[i] = po[2].y; } } void merge(int x,int y) { if(Find(x) == Find(y)) return; x = Find(x), y = Find(y); p[x] = y; szp[y] += szp[x]; } void dfs_union(int x,int pp) { for(int i : tv[x]) { if(i == pp) continue; merge(i,x); dfs_union(i,x); } } void dfs_com(int x) { if(c[x]) return; c[x] = 1; dfscom.push_back(x); for(int i : v[x]) dfs_com(i); } vec find_split(int N,int AA,int BB,int CC,vec vec1,vec vec2) { n = N, A = AA, B = BB, C = CC; m = vec1.size(); po[0] = {A,1}, po[1] = {B,2}, po[2] = {C,3}; sort(po,po+3); a = po[0].x, b = po[1].x; ans.resize(n,0); init_union(); for(int i = 0;i < m;i++) { int x = vec1[i], y = vec2[i]; rv[x].push_back(y); rv[y].push_back(x); if(Find(x) == Find(y)) continue; merge(x,y); tv[x].push_back(y); tv[y].push_back(x); } int cent = make_cent(); get_sz(cent,-1); init_union(); for(int i : tv[cent]) { dfs_union(i,cent); if(szp[Find(i)] >= a) { canDo(cent,i); return ans; } } for(int i = 0;i < m;i++) { if(vec1[i] == cent||vec2[i] == cent) continue; int x = vec1[i], y = vec2[i]; if(Find(x) != Find(y)) { v[p[x]].push_back(p[y]); v[p[y]].push_back(p[x]); } } memset(c,0,sizeof(c)); for(int i = 0;i < n;i++) { if(i == cent||Find(i) != i||c[i]) continue; dfscom.clear(), com.clear(); dfs_com(i); int sum = 0; for(int j : dfscom) { sum += szp[j]; com.push_back(j); if(sum >= a) break; } if(sum >= a) { int st = -1; for(int j : com) ch[j] = 1; for(int j = 0;j < n;j++) { if(ch[Find(j)]) { poham[j] = 1; st = j; } } cnt = 0, cntmx = a; dfs_pofill(st,po[0].y,1); cnt = 0, cntmx = b; dfs_pofill(cent,po[1].y,0); for(int j = 0;j < n;j++) { if(!ans[j]) ans[j] = po[2].y; } return ans; } } return ans; }
#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...