제출 #1242618

#제출 시각아이디문제언어결과실행 시간메모리
1242618bangan참나무 (IOI23_beechtree)C++20
0 / 100
2103 ms248828 KiB
#include "beechtree.h" #include <bits/stdc++.h> using namespace std; #define pb push_back #define ALL(a) a.begin(), a.end() const int MXN = 200200; int n; int m; vector<int> p; vector<int> c; vector<int> adj[MXN]; bool leaf[MXN]; int h[MXN]; int sz[MXN]; vector<int> sub[MXN]; int ptr[MXN]; bool valid(vector<int> vec) { bool ans = true; vector<int> R(vec.size(), -1); for (auto it = next(vec.begin()); it != vec.end(); it++) { if (vec.size() <= ptr[c[*it]]) ans = false; if (R[ptr[c[*it]]] != p[*it]) { if (R[ptr[c[*it]]] != -1) ans = false; else if (h[vec[ptr[c[*it]]]] != h[p[*it]] || sz[vec[ptr[c[*it]]]] != sz[p[*it]]) ans = false; else R[ptr[c[*it]]] = p[*it]; } ptr[c[*it]]++; } for (int v : vec) ptr[c[v]] = 0; return ans; } bool is_subset(int v, int u) { bool ans = true; for (int x : sub[u]) ptr[c[x]]++; for (int x : sub[v]) { ptr[c[x]]--; if (ptr[c[x]]<0) ans = false; } for (int x : sub[u]) ptr[c[x]]=0; for (int x : sub[v]) ptr[c[x]]=0; return ans; } bool cmp(int v, int u) { if (h[v]!=h[u]) return h[v]<h[u]; else if (sz[v]!=sz[u]) return sz[v]>sz[u]; else return is_subset(v, u); } vector<int> ans; vector<int> dfs(int v) { sz[v] = 1; if (leaf[v]) { ans[v] = 1; return {v}; } vector<int> norm{v}, esp; for (int u : adj[v]) { h[u] = h[v]+1; auto vec = dfs(u); sz[v] += sz[u]; for (int x : vec) (leaf[x] ? esp : norm).pb(x); } sort(ALL(norm), cmp); sort(ALL(esp), [&](int x, int y) { return cmp(p[x], p[y]); }); vector<int> res; for (int x : norm) res.pb(x); for (int x : esp) res.pb(x); sort(ALL(res), cmp); ans[v] = valid(res); return sub[v] = res; } vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) { n = N; m = M; p = P; c = C; for (int i=1; i<n; i++) adj[p[i]].pb(i); for (int i=0; i<n; i++) leaf[i] = adj[i].empty(); ans.resize(n); dfs(0); 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...
#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...