QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#696895 | #9532. 长野原龙势流星群 | hos_lyric# | 0 | 323ms | 39420kb | C++14 | 5.2kb | 2024-11-01 06:01:20 | 2024-11-01 06:01:21 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
vector<int> uf;
int root(int u) {
return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
u = root(u);
v = root(v);
if (u == v) return false;
if (uf[u] > uf[v]) swap(u, v);
uf[u] += uf[v];
uf[v] = u;
return true;
}
struct Heap {
Heap *l, *r;
// Int key;
Int p, q;
int u;
// Heap(Int key_ = 0) : l(nullptr), r(nullptr), key(key_) {}
Heap() : l(nullptr), r(nullptr), p(0), q(0) {}
};
bool operator<(const Heap &a, const Heap &b) {
return ((__int128)a.p * b.q < (__int128)b.p * a.q);
}
// max heap
Heap *meld(Heap *a, Heap *b) {
if (!a) return b;
if (!b) return a;
// if (a->key < b->key) swap(a, b);
if (*a < *b) swap(a, b);
a->r = meld(a->r, b);
swap(a->l, a->r);
return a;
}
void pop(Heap *&a) {
a = meld(a->l, a->r);
}
int N;
vector<int> P;
vector<Int> W;
vector<double> ans;
vector<vector<int>> graph;
vector<int> sz, del;
void dfsSz(int u, int p) {
sz[u] = 1;
for (const int v : graph[u]) if (p != v) {
dfsSz(v, u);
sz[u] += sz[v];
}
}
string dfsString(int u, int p) {
ostringstream oss;
oss << "[" << u;
for (const int v : graph[u]) if (!del[v] && p != v) {
oss << " " << dfsString(v, u);
}
oss << "]";
return oss.str();
}
/// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
vector<Heap> nodes;
vector<int> us;
vector<vector<int>> uss;
Heap *dfs(int u, int p) {
us.push_back(u);
uf[u] = -1;
Heap *ret = nullptr;
for (const int v : graph[u]) if (!del[v] && p != v) {
Heap *res = dfs(v, u);
ret = meld(ret, res);
}
Heap *a = &nodes[u];
*a = Heap();
a->p = W[u];
a->q = 1;
a->u = u;
for (; ret && *a < *ret; pop(ret)) {
a->p += ret->p;
a->q += ret->q;
connect(u, ret->u);
}
ret = meld(ret, a);
return ret;
}
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void solveSubtree(int depth, int r) {
#ifdef LOCAL
cerr << string(2 * depth, ' ') << "solveSubtree " << dfsString(r, -1) << endl;
#endif
vector<int> vs;
for (const int v : graph[r]) if (!del[v]) {
vs.push_back(v);
}
const int len = vs.size();
/// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
us.clear();
Heap *res = dfs(r, -1);
for (const int u : us) uss[u].clear();
for (const int u : us) uss[root(u)].push_back(u);
Int p = 0, q = 0;
for (; res; pop(res)) {
// cerr<<string(2*depth,' ')<<res->p<<" "<<res->q<<" "<<res->u<<endl;
p += res->p;
q += res->q;
const double avg = (double)p / q;
for (const int u : uss[root(res->u)]) {
chmax(ans[u], avg);
}
}
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
void solveRec(int depth, int u) {
for (; ; ) {
int vm = -1;
for (const int v : graph[u]) if (!del[v]) {
if (!~vm || sz[vm] < sz[v]) {
vm = v;
}
}
if (!~vm || 2 * sz[vm] <= sz[u]) {
solveSubtree(depth, u);
del[u] = 1;
for (const int v : graph[u]) if (!del[v]) {
solveRec(depth + 1, v);
}
break;
} else {
sz[u] -= sz[vm];
sz[vm] += sz[u];
u = vm;
}
}
}
void centroidDecomp() {
sz.assign(N, 0);
dfsSz(0, -1);
del.assign(N, 0);
solveRec(0, 0);
}
int main() {
for (; ~scanf("%d", &N); ) {
P.assign(N, -1);
for (int u = 1; u < N; ++u) {
scanf("%d", &P[u]);
--P[u];
}
W.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%lld", &W[u]);
}
graph.assign(N, {});
for (int u = 1; u < N; ++u) {
graph[P[u]].push_back(u);
graph[u].push_back(P[u]);
}
uf.assign(N, -1);
nodes.assign(N, Heap());
uss.assign(N, {});
ans.assign(N, 0.0);
centroidDecomp();
for (int u = 0; u < N; ++u) {
printf("%.10f\n", ans[u]);
}
}
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 4288kb
input:
2000 1 2 2 4 5 2 3 6 4 2 7 2 8 14 8 12 1 14 4 14 8 18 9 2 7 22 20 22 14 29 28 16 6 21 23 6 21 14 13 9 1 4 18 13 2 39 21 33 18 20 38 27 27 1 49 5 51 3 31 24 10 42 2 44 13 9 35 66 27 60 67 59 29 40 53 2 33 43 26 43 62 16 78 45 14 10 73 69 41 35 25 26 2 70 54 1 54 48 5 36 44 28 90 29 51 51 93 82 95 45 ...
output:
882257050.6034482718 887174926.0000000000 883143065.9795918465 912609654.6666666269 891981335.5000000000 863131520.5277777910 875309040.9212598801 892392319.1666666269 863131520.5277777910 879583261.3157894611 797359285.8495867252 925100890.0000000000 994728511.5000000000 950304719.0000000000 887261...
result:
wrong answer 1st numbers differ - expected: '883838885.9230770', found: '882257050.6034483', error = '0.0017897'
Subtask #2:
score: 0
Wrong Answer
Test #32:
score: 0
Wrong Answer
time: 323ms
memory: 39420kb
input:
200000 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...
output:
792545632.4154930115 794937150.3513513803 792545632.4154930115 804131414.2264150381 805846267.1666666269 806376230.0000000000 787989590.3426573277 815562308.1499999762 783684808.6881959438 809328819.5882352591 775543719.7207074165 778371224.6485356092 800202632.8939393759 879639965.3333333731 781605...
result:
wrong answer 3rd numbers differ - expected: '794843085.4776120', found: '792545632.4154930', error = '0.0028904'
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #2:
0%